Python is an Object-Oriented Programming (OOP) language, and every element (attributes, functions, etc.) in the python program is an object of a class.
For example, if you use any element such as string, number, list, etc., in a python program, those are the objects of built-in classes.
The above example will return the result as shown below.
In the programming world, class is a collection of various data members such as properties, attributes, functions, etc., and the object is an instance of a class to access the defined data members from the class. We can also say the object is the blueprint of a class.
Generally, the OOP concepts are useful to create reusable code by reducing code duplication. Now, we will learn the class, the object, and how to use classes and objects in python with examples.
In python, the class is a combination of data members such as constructors, properties, attributes, etc., and member functions.
To create a class in python, you need to use the class
keyword as shown below.
Following is the example of creating a class in python.
If you observe the above python example, we created a class(user) using a class
keyword with required variables and functions.
The self
keyword in python is useful to refer to the current instance of the class. To access the class level variables, we used the self
keyword in the userdetails() function.
As discussed, the object is an instance of a class to access the defined data members. The object will act as a blueprint of a class.
We can create the object of the above user class as shown below.
After creating the object for the user class, you can access the class properties as shown below.
Following is the example of creating the object and accessing the class properties in python.
The above example will return the result as shown below.
In python, the constructor is a method that will invoke automatically whenever an instance of a class is created.
To create the constructor in a class, you need to use __init()__
method with self
parameter as shown below.
As discussed, the self
keyword will refer to the current instance of a class. To access the class level variables, we used the self
keyword in the constructor method (__init__
).
Whenever an instance of a user class is created, the constructor method (__init__
) will execute automatically.
Following is the example of creating a class with constructor in python.
The above example will return the result as shown below.
In python, creating a class with no content is not allowed. To create an empty class in python, you need to use the pass
statement in a class definition.
Following is the example of creating an empty class in python using the pass
statement.
If required, you can modify the object attributes as shown below.
The above example will return the result as shown below.
By using the del
keyword, you can delete the object attributes as shown below.
When you execute the above program, you will get the result as shown below.
You can also delete the complete object using the del
keyword as shown below.
This is how you can create the classes and objects in python to access the defined properties based on your requirements.