Python programming language is an object-oriented programming language.
Almost everything in Python is an object with properties and methods.
Classes are like object constructors or blueprints for creating objects.
Using the class
keyword, you can create a Python class.
class PythonClass:
color = "Red"
In the above example, we have created the Python class (PythonClass
) using the class
keyword and declared one property (color
) within the Python class.
The PythonClass
was used as a class name here, but you can use any name you like.
Use the Python class name to create a Python class object.
class PythonClass:
color = "Red"
obj = PythonClass()
print(obj.color)
Red
Here, a Python class (PythonClass()
) object is created and assigned that object to an obj
reference variable. Using the obj
reference variable, we called the color
property from the Python class.
The __init__() function is called a constructor function.
When a class is being initiated, a function called __init__() is always executed.
If you need to assign values to object properties or perform other operations when creating an object, you can use the __init__() function.
class PythonClass:
def __init__(self, color):
self.color = color
obj = PythonClass("Red")
print(obj.color)
Red
In this example, the __init__() function is declared within a Python class (PythonClass()
). This __init__() function specifies the self
and color
parameters, where color
is a regular parameter variable, and the self
parameter is a reference to the current class instance.
While creating the Python class (PythonClass()
) object, it is necessary to pass as an input argument to the Python class. Otherwise, it will give an error. Here we passed the "Red"
color as an input argument to the Python class object.
Note :- Whenever the class is used to create a new object, it automatically calls __init__() function.
Note :- The self-parameter is a reference to the current class instance and can be used to access class variables.
The string representation of a class object is controlled by the __str__() function.
When you try to print a class object without the __str__() function, it prints the class object as a string.
class PythonClass:
def __init__(self, color):
self.color = color
obj = PythonClass("Red")
print(obj)
<__main__.PythonClass object at 0x10a225760>
As a result of printing the class object, we got the output class object as a string.
class PythonClass:
def __init__(self, color):
self.color = color
def __str__(self):
return f"The color name is {self.color}."
obj = PythonClass("Red")
print(obj)
The color name is Red.
Here, we controlled the output using the __str__() function and returned a simple text while trying to print the class object.
It is possible to create multiple functions in the Python class without any restrictions.
Whenever a function is created in a Python class, its first parameter must always be the current class instance.
class PythonClass:
def __init__(self, color):
self.color = color
def color_function(self):
return f"The color name is {self.color}."
obj = PythonClass("Red")
print(obj.color_function())
The color name is Red.
In the above example, we called a class function (color_function()
) that is declared in the class (PythonClass
) with the help of a class object.
The self-parameter is a reference to the current class instance and can be used to access class variables.
It is not necessary to use self as a parameter name, you may use any name you like.
class PythonClass:
def __init__(abc, color):
abc.color = color
def color_function(pqr):
return f"The color name is {pqr.color}."
obj = PythonClass("Red")
print(obj.color_function())
The color name is Red.
As a class instance reference, we used the abc and pqr names instead of self.
There are no restrictions on changing object properties multiple times.
class PythonClass:
def __init__(self, color):
self.color = color
def color_function(self):
return f"The color name is {self.color}."
obj = PythonClass("Red")
obj.color = "Blue"
print(obj.color_function())
The color name is Blue.
You can also delete object properties using the del
keyword.
class PythonClass:
def __init__(self, color):
self.color = color
def color_function(self):
return f"The color name is {self.color}."
obj = PythonClass("Red")
del obj.color
print(obj.color_function())
AttributeError: 'PythonClass' object has no attribute 'color'
We get an error if we attempt to fetch the property after it has been deleted.
By using the del
keyword, you can delete an object.
class PythonClass:
def __init__(self, color):
self.color = color
def color_function(self):
return f"The color name is {self.color}."
obj = PythonClass("Red")
del obj
print(obj.color_function())
NameError: name 'obj' is not defined
We get an error while trying to fetch the function from the object after deleting an object.
The class
in Python should not be empty, if they are empty for any reason, use a pass
statement within them to avoid getting an error.
class PythonClass:
pass
Above example, we used a pass
statement to avoid an error.