Object Attributes
Updated
•1 min readProblem Description
Write a program to create a class and print attributes using a method of the class.
Create a class
Create a class named
Bicycle.Inside this class, create two methods named:
__init__()andprint_attributes().The class will have two attributes:
gearandspeedwhich should be initialized inside__init__().Inside the
print_attributes()method, print thegearattribute followed by thespeedattribute in two separate lines.
Outside of the class
Create an object named
bicycle1of theBicycleclass. Thegearandspeedattributes of this object should be 4 and 80 respectively.Call the
print_attributes()method using thebicycle1object.
# create the class
class Bicycle:
def __init__(self, gear, speed):
# initialize attributes
self.gear = gear
self.speed = speed
# create the print_attributes() method
def print_attributes(self):
print(self.gear)
print(self.speed)
# create the object with 4 and 80 as arguments
bicycle1 = Bicycle(4,80)
# call print_attributes() using bicycle1
bicycle1.print_attributes()