45 Creating A Class
45 Creating A Class
def __init__(self):
"""Constructor"""
pass
def __init__(self):
"""Constructor"""
pass
You will notice that the only difference is that we no longer need the
parentheses if we’re basing our class on object. Let’s expand our class
definition a bit and give it some attributes and methods.
class Vehicle(object):
"""docstring"""
def brake(self):
"""
Stop the car
"""
return "Braking"
def drive(self):
"""
Drive the car
"""
return "I'm driving!"
The code above added three attributes and two methods. The three attributes
are:
self.color = color
self.doors = doors
self.tires = tires
Attributes describe the vehicle. So the vehicle has a color, some number of
doors and some number of tires. It also has two methods. A method describes
what a class does. So in this case, a vehicle can brake and drive. You may
have noticed that all of the methods, including the first one have a funny
argument called self. Let’s talk about that!