We are going to follow the exercise instructions step by step and give solutions to each step, so you canfollow it easily.Also, we will review the conceps shown in classes to fix them.
Defining a class is much like defining a function, but we use the class keyword instead. We also use the word object in parentheses because we want our classes to inherit the object class. This means that our class has all the properties of an object, which is the simplest, most basic class.
Define a new class named "Car". For now, since we have to put something inside the class, use the pass keyword.
We can use classes to create new objects, which we say are instances of those classes.
Below your Car class, create a new object named my_car
that is an instance of Car.
Solution 1
>>> class Car(object):
... pass
...
... my_car=Car()
>>>
Classes can have member variables that store information about each class object. We call them member variables since they are information that belongs to the class object.
my_car
.Solution2
>>>
>>> class Car(object):
... condition="new"
...
>>> my_car=Car()
>>> print my_car.condition
new
>>>
There is a special function named __init__() that gets called whenever we create a new instance of a class.The first argument passed to
init() must always be the keyword
self - this is how the object keeps track of itself internally - but we can pass additional variables after that.In order to assign a variable to the class (creating a member variable), we use dot notation.
Define the __init__()
function of the Car class to take four inputs: self, model, color, and mpg. Assign the last three inputs to member variables of the same name by using the self keyword.
Then, modify the object my_car to provide the following inputs at initialization:
model = "DeLorean"
color = "silver"
mpg = 88
Solution 3
>>> class Car(object):
... condition="new"
... def __init__(self,model,color,mpg):
... self.model=model
... self.color=color
... self.mpg=mpg
...
>>>
>>>
>>>
>>> my_car=Car("DeLorean","silver",88)
>>> print my_car.condition
new
>>>
Calling class member variables works the same whether those values are created within the class (like our car's condition) or values are passed into the new object at initialization. We use dot notation to access the member variables of classes since those variables belong to the object.
my_car
.
Then print out the color of my_car
.Finally, print out the mpg of `my_car.Solution 4
>>> class Car(object):
... condition="new"
... def __init__(self,model,color,mpg):
... self.model=model
... self.color=color
... self.mpg=mpg
...
>>> my_car=Car("DeLorean","silver",88)
>>> print my_car.model
DeLorean
>>> print my_car.color
silver
>>> print my_car.mpg
88
>>>
Besides member variables, classes can also have their own methods (functions inside the class).
display_car()
to Car that will reference the Car's member variables to return the string, "This is a [color] [model] with [mpg] MPG." You can use the str()
function to turn your mpg into a string when creating the display string.my_car.display_car()
.Solution 5
>>> class Car(object):
... condition = "new"
... def __init__(self, model, color, mpg):
... self.model = model
... self.color = color
... self.mpg = mpg
...
... def display_car(self):
... return "This is a %s %s with %s MPG." % (self.color,self.model,str(self.mpg))
...
>>>
>>> my_car = Car("DeLorean", "silver", 88)
>>> print my_car.display_car()
This is a silver DeLorean with 88 MPG.
>>>
We can modify variables that belong to a class the same way that we initialize those member variables.
drive_car()
that sets self.condition to the string "used".my_car.display_car()
and instead print only the condition of your car.drive_car()
method.Solution 6
>>> class Car(object):
... condition = "new"
... def __init__(self, model, color, mpg):
... self.model = model
... self.color = color
... self.mpg = mpg
...
... def display_car(self):
... return "This is a %s %s with %s MPG." % (self.color,self.model,str(self.mpg))
...
... def drive_car(self):
... self.condition="used"
...
>>>
>>>
>>> my_car = Car("DeLorean", "silver", 88)
>>> print my_car.condition
new
>>> my_car.drive_car()
>>> print my_car.condition
used
>>>
>>>
__init__()
method of that includes a "battery_type" member variable in addition to the model, color and mpg.battery_type.
Supply values of your choice for the other three inputs (model, color and mpg).Solution 7
>>> class Car(object):
... condition = "new"
... def __init__(self, model, color, mpg):
... self.model = model
... self.color = color
... self.mpg = mpg
...
... def display_car(self):
... return "This is a %s %s with %s MPG." % (self.color,self.model,str(self.mpg))
...
... def drive_car(self):
... self.condition="used"
...
>>>
>>>
>>> class ElectricCar(Car):
... def __init__(self,model, color, mpg, battery_type):
... self.model = model
... self.color = color
... self.mpg = mpg
... self.battery_type=battery_type
...
>>>
>>>
>>>
>>>
>>> my_car=ElectricCar("DeLorean", "silver", 88,"molten salt")
>>>
my_car
.my_car
by calling the drive_car()
function.my_ca
r againSolution 8
>>>
>>> class Car(object):
... condition = "new"
... def __init__(self, model, color, mpg):
... self.model = model
... self.color = color
... self.mpg = mpg
...
... def display_car(self):
... return "This is a %s %s with %s MPG." % (self.color,self.model,str(self.mpg))
...
... def drive_car(self):
... self.condition="used"
...
>>>
>>> class ElectricCar(Car):
... def __init__(self,model, color, mpg, battery_type):
... self.model = model
... self.color = color
... self.mpg = mpg
... self.battery_type=battery_type
...
... def drive_car(self):
... self.condition="like new"
...
>>>
>>>
>>> my_car=ElectricCar("DeLorean", "silver", 88,"molten salt")
>>> print my_car.condition
new
>>> my_car.drive_car()
>>> print my_car.condition
like new
>>>