A few examples of a class called Car. First the simplest class definition:
>>> class Car:
... pass
...
>>> a = Car()
>>> a
<__main__.Car object at 0x0000023848FB4E80>
>>> b = Car()
>>> a == b
FalseHere a and b are instances of a Car class. They differ as they are separate blocks of memory.
Convention Note: Python classes (but not variables or functions) use CamelCase, namely with capital letters in each word like this: SportsCar.
The Python file car.py in the python folder has an example of a more involved Car class:
class Car:
n_wheels = 4
def __init__(self, make):
self.make = make
def __str__(self):
return f'{self.make} with {self.n_wheels} wheels'
def my_needs(self):
print('I need a tank of fuel')Notice the 4 components.
- The first line or lines after the
classdefinition contain the class attributes. In this casen_wheelsis a class attribute and will be4for all instances of the class. - The
__init__(self, make)is a special function that initializes the class.selfis a special parameter that is automatically provided and refers to the instance itself. The remaining parameters are provided on instantiation. Providing this overrides the default__init__that Python assigns each class. - The
__str__(self)is a special optional function the must return a string. This is whatprint()receives when it is called on a class instance. Again, providing this overrides the default__str__function that Python assigns each class. - The
my_needs(self)function is a method of this class.
To try out this class, copy and paste the above lines into an interactive terminal. They try the following:
>>> car1 = Car('Ford')We have created a Car instance with make being Ford. Now the following function calls the __str__ method:
>>> print(car1)
Ford with 4 wheelsWe can also call other methods:
>>> car1.my_needs()
I need a tank of fuelA child class inherits the attributes and methods of its parent class. The child class can override these attributes and methods, and it can add additional attributes and methods. The following is a simple example from the car.py file:
class ElectricCar(Car):
def my_needs(self):
print(f'I need a {self.make} supercharger')The command class ElectricCar(Car) causes the ElectricCar class to inherit all the attributes and methods of the Car class. Within this class definition we have re-defined one of the methods my_needs(). This overrides the method of the same name defined in the parent class. Let's try it out by copy and pasting the above into your interactive terminal, and then doing the following:
>>> car2 = ElectricCar('Tesla')
>>> print(car2)
Tesla with 4 wheels
>>> car2.my_needs()
I need a Tesla superchargerNotice that the my_needs() function is defined by the child class.
The isintance method shows that car2 is both a Car and an ElectricCar:
>>> isinstance(car2,Car)
True
>>> isinstance(car2,ElectricCar)
True
>>> isinstance(car1,ElectricCar)
False