10. Classes
In this lesson you will learn about what makes Python an object oriented programming language (OOP).
Classes and Objects
Almost everything in Python is an object. A class is like an object's "blueprint" for creating objects.
To create a class, use the keyword "class" followed by the name of the class. Do not forget to indent the class and to put a colon after the name of the class. Here is an example of a class named MyFirstClass with a property named a:
class MyFirstClass:
a = 123
The names of classes typically start with a capital letter and separate the words in the name with the capitalized letter of the beginning of the next word.
To create an object it is very similar to creating variables but set the object equal to the name of the class followed by parentheses. Here is an example using the MyFirstClass class:
class MyFirstClass:
a = 123
abc = MyFirstClass()
print(abc.a) # 123
Note how you can access properties of objects when a dot. The print statement print's the object abc's property a.
When objects are initially created a constructor method runs. A method is a function in an object. The constructor method is called __init__(). This method can be used to assign values to the object's properties when it is created. Here is an example:
class Car:
def __init__(self, model, color, year):
self.model = model
self.color = color
self.year = year
truck = Car("tacoma", "red", 2008)
print(truck.model) # tacoma
print(truck.color) # red
print(truck.year) # 2008
Note how you always have a self parameter and set the properties with self.property. The self parameter can be named anything.
Objects can also contain methods. Remember methods are just functions inside of an object. Here is an example of a method inside of the Car class:
class Car:
def __init__(self, model, color, year):
self.model = model
self.color = color
self.year = year
def honk(self):
print("HONK")
truck = Car("tacoma", "red", 2008)
truck.honk() # HONK
Notice how methods need a self parameter.
The self parameter inside of an object is used to reference the current instance of the class and is used to access variables that belong to the class.
To modify properties of an object just set it like you would for a variable. Here is an example:
class Car:
def __init__(self, model, color, year):
self.model = model
self.color = color
self.year = year
truck = Car("tacoma", "red", 2008)
print(truck.color) # red
truck.color = "blue"
print(truck.color) # blue
Inheritance
Inheritance allows classes to inherit all the methods and properties of another class. A parent class is a class being inherited from and a child class is the class that inherits from another class. To create a child class, put the name of the parent class inside of the parentheses. Here is an example of a parent and child class:
class Vehicle:
def __init__(self, model, color, year):
self.model = model
self.color = color
self.year = year
def get_color(self):
return self.color
class Truck(Vehicle):
pass
tacoma = Truck("tacoma", "red", 2008)
print(tacoma.get_color()) # red
When there is just a pass statement inside of a child object, no objects or method are added to the class.
When you add an __init__() method to the child class, it no longer inherits the parent's __init__ method. In other words, the child's __init__() method overrides the parent's. To call the parent's __init__() method use the super() function. This will call the method as if it was the parent. Below is an example:
class Vehicle:
def __init__(self, model, color, year):
self.model = model
self.color = color
self.year = year
class Truck(Vehicle):
def __init__(self, model, color, year):
super().__init__(model, color, year)
self.speed = "slow"
tacoma = Truck("tacoma", "red", 2008)
print(tacoma.model) # tacoma
print(tacoma.color) # red
print(tacoma.year) # 2008
print(tacoma.speed) # slow
❮ PreviousNext ❯