Python Classes
Python is an object-oriented language and the object-oriented approach is one of the best approaches to write software. Classes represent real-life things, you then create objects based on these classes. This will make more sense as we will learn more about Python Classes.
An object is a collection of variables and Python functions that will act on those variables. A class is just a map of the object. Objects in a class are also called instances of that class and when we create a new object in a class, the process is called instantiation in Python classes.
A class is an important part of programming because it can be used over and over again, therefore you will not have to write different functions again. They are very useful in a team project, where different parts of the code can be categorized into different classes and the whole team can use it.
Classes are like models or blueprints of a real-life environment or situation, let us see how you can create a class in Python.
Python Class Creation
A class in Python is created using the class keyword, the name of the class follows the keyword immediately and the whole syntax is ended using the colon (:).
Syntax
class Name_of_class: “”” A docstring “”” Components
The word “Components” is used here to describe all of the functions and variables that the class is going to contain.
It can be accessed using the method __doc__ that we will see in the latter part of the article. Literally, anything can be modeled using a class, let us create a class named automobile that will store information about different automobiles.
All examples shown below are a part of a larger code and most of the examples will not work in the compiler without the other parts of the code.
Example
For now, the code you read above does not print anything.
To use this function in the class, we will have to create an object to access it, let us see how we can do that.
So let us see how to use the class above to print the information about the automobile.
The __init__() method
Before we understand what is an __init__() method in the class, let us get to understand what is a method first.
A method is basically a function that is part of a class, the only difference between a method and a function is the way they are called or used.
The __init__ method is a very special method that gets executed automatically whenever we create an object based on that class. There are two leading and two trailing underscores in the word “__init__”.
The methods in a class that gets executed automatically are called python class constructors.
Let’s see how the __init__() method is working in the class above –
- First, we have passed three parameters in the method, i.e., self, manufacturer, model. The “self” parameter must be included while defining the method and it also should be written before all of the parameters.
- Then we define/initialize two variables that have the prefix “self”. The “self” prefix means that those two variables are available to all of the methods in the class.
- Next, we define two more methods called “electric” and “petroleum”, these two new methods only prints a message to the screen, so we only pass a parameter “self” which makes them available to the objects that we will create afterward.
Now to use all of these methods, we will have to create objects.
Classes and Objects
Every object shares the attributes of the class and the values of those attributes are different for each object. Unlimited objects can be created for a single class.
The creation or process of creating an object is kind of similar to that of a function call in Python. Have a look at how you can create an object or an instance of a class.
Example
So exactly what is happening here?
- car1 is the name of our object for the class, and we pass the information about the car to it.
- When Python executes this command, the __init__() method also gets executed and the information passed in the object gets assigned to the attributes.
- Now that the value has been assigned to the attributes of the class, we print it by accessing the attributes using the dot (.) operator.
Docstrings can be printed using this syntax:
print( classname.__doc__ )
Example
Accessing Methods
We successfully have created an object to access attributes in the class, but now we also want to access the different methods i.e., electric() and petroleum() that we have created in the class.
This can also be done with the help of the already present object using the dot notation like this
Example
Our first car runs on petroleum fuel, so we only use the second function to print the necessary function on the screen.
However, the first method can also be used in a similar way. For that, let us create another object for the class
Example
Attributes in Class
Different attribute values
You can create different objects for a class and those objects can have different values for the class attributes, this one property of classes makes them very usable at team projects that require lots of code.
We will use the already created objects car1 and car2 to demonstrate how the attribute value can be changed using different objects-
Example
Default values of attributes
An attribute in class must have an initial value, you can set an initial default value for an attribute in the __init__() method.
Example
Above we created an __init__() method to initialize our attributes, it does not print anything to the screen.
Now we will create two methods that will print product details and storage temperature.
Example
After creating these two methods, now let us create an object to use those methods.
Example
You can modify the value of the attributes like this:
Example
Attributes of an object in a class can be deleted using the del operator like this:
Example
The code above will result in error because the value of storage_temperature no longer exists.