Python is an incredibly powerful language. Learning how to create Python class variables is part of developing an object-oriented programming method. Creating individual objects from the class allows you to give each object whatever unique traits you choose.
Making an object from a class is called instantiation. This lesson will teach you how to create class variables and instances of those classes in Python. Classes also make programming in Python easier and help when your programs grow in complexity.
We will create a simple class as a Car. This class variable of Car represents all cars. Cars have brands, ages, colors, etc. Cars also move forward and reverse.
Create the Car class variables
The __init__() method is a special method that Python runs whenever you create a new instance based on the Car class.
class Car: def __init__(self, brand, year): #Initialize brand and year attributes self.brand = brand self.year = year def forward(self): #car will go forward print(f"{self.brand} is going forward.") def reverse(self): #car goes in reverse print(f"{self.brand} went in reverse")
This method has two leading underscores and two trailing underscores. This is used to prevent Python’s default method names from conflicting with your method names. Make sure to use two underscores on each side of __init__().
__init__() has three parameters: self, brand, and year.
A few rules to follow:
- The self parameter is required in the method definition and must come first before the other parameters.
- It must be included in the definition because when Python calls this method later (to create an instance of Car), the method call will automatically pass the self argument.
- Every method call associated with an instance automatically passes self, which is a reference to the instance itself; it gives the individual instance access to the attributes and methods in the class. When we make an instance of Car, Python will call the __init__() method from the Car class.
- We’ll pass Car() a brand and a year as arguments; self is passed automatically, so we don’t need to pass it. Whenever we want to make an instance from the Car class, we’ll provide values for only the last two parameters, brand and year.
The line self.brand = brand takes the value associated with the parameter brand and assigns it to the variable brand, which is then attached to the instance being created. The same process happens with self.year = year.
The Car class has two other methods defined: forward() and reverse(). For now, forward() and reverse() don’t do much other than print a message saying the car is going forward or in reverse.
Making an Instance from a Class
Now we can make an instance of the class which is essentially giving instructions. The class Car comes with instructions that will let Python know how to make individual instances representing specific cars.
Let’s make an instance representing a specific car:
my_car = Car('Tesla', 2018) print(f"My car is a {my_car.brand}.") print(f"My car is a {my_car.year} {my_car.brand}.")
When Python reads this line, it calls the __init__() method in Car with the arguments ‘Tesla’ and 2018. The __init__() method creates an instance representing this particular car and sets the brand and year attributes using the values we provided. Python then returns an instance representing this car. We assign that instance to the variable my_car. The naming convention is helpful here: we can usually assume that a capitalized name like Car refers to a class, and a lowercase name like my_car refers to a single instance created from a class.
See full code below:
https://gist.github.com/craine/bdffa0a1af6ff20c298a76ff070c4204