Hi everyone, In this tutorial, we will understand the concept of Classes and Objects in Python using real-world examples.
1. What are the Python classes?
According to the official documentation of the classes
in Python, Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it for maintaining its state. Class instances can also have methods (defined by its class) for modifying its state.
The above definition simply means that Classes provides a blueprint or an environment having some properties and behaviours that are applicable to all the objects or instances of that class.
Let us take an example of a Car manufacturer company say, OtpCars, All the cars manufactured by a class OtpCars may have some properties or attributes and some methods which can be used to modify the attributes of each car. For now, let’s say we have to define an empty class named OtpCars.
2. Python Class Syntax
Classes in python are defined using the class
keyword followed by the name of the class
and a colon:
at the end. After that, we can declare attributes
and methods
that we want to have in our class or we can use the pass
keyword to declare an empty class.
class ClassName:
data member/attributes
methods()
Let’s create an empty class named OtpCars as per our example above.
class OtpCars:
pass
3. What are the objects?
As per our example, only creating a class of car manufacturer won’t do anything unless it actually manufactures a real-world entity i.e cars. Similarly, We need to create these real-world entities or simply objects which are theinstances of that class
and having the attributes defined in the class definition.
Now we can create as many objects or cars (as per example) as we want that belongs to our class, OtpCars (as per example). Let’s see the syntax of how to initialize Objects of a class.
3.1 Syntax for initializing objects in Python
The basic syntax of initializing objects is by calling class constructor
and assigning it to the object name
. There can be arguments
required to pass inside constructor that depends on how the class constructor is defined in the class definition.
Go through this tutorial to learn more about constructors in Python.
objName = className(*arguments)
Let’s create two objects of our class OtpCars and refer to them as car1 and car2.
class OtpCars:
pass
if __name__ == "__main__":
# initializing Objects
car1 = OtpCars()
car2 = OtpCars()
# printing the type of Objects
print(type(car1))
print(type(car2))
<class '__main__.OtpCars'>
<class '__main__.OtpCars'>
4. Adding Data members and Constructor in the class
class OtpCars:
seating = 5
base_color = "white"
sunroof = False
def __init__(self,name):
self.name=name
if __name__ == "__main__":
# initializing Objects
car1 = OtpCars("ABC")
car2 = OtpCars("XYZ")
print("Properties of car1")
print("car1 Brand name: ",car1.name)
print("car1 Base Color: ",car1.base_color)
print("car1 seating: ",car1.seating)
print("car1 Sunroof present: ",car1.sunroof)
print("\nProperties of car2")
print("car2 Brand name: ",car2.name)
print("car2 Base Color: ",car2.base_color)
print("car2 seating: ",car2.seating)
print("car2 Sunroof present: ",car2.sunroof)
Properties of car1
car1 Brand name: ABC
car1 Base Color: white
car1 seating: 5
car1 Sunroof present: False
Properties of car2
car2 Brand name: XYZ
car2 Base Color: white
car2 seating: 5
car2 Sunroof present: False
5. Adding Member functions and Methods in the class
We see that we have made 2 cars belongs to the company OtpCars or 2 objects that are instances of the same class having similar attributes. But every time we create a car they all may have colour white and capacity of 5. To make changes in these attributes, Let us define two methods inside the class, one for changing the colour and other to add a sunroof on the top.
Suppose the buyer of car1 wants to change the colour of car and buyer of car2 wants to have a sunroof. In this case, we can use these methods to modify the behaviour or state of the objects. Let’s see the code.
class OtpCars:
seating = 5
base_color = "white"
sunroof = False
def __init__(self,name):
self.name=name
def changeColor(self,new_color):
self.base_color=new_color
def makeSunroof(self):
self.sunroof=True
if __name__ == "__main__":
# initializing Objects
car1 = OtpCars("ABC")
car2 = OtpCars("XYZ")
# Changing the behaviour of objects
car1.changeColor("black")
car2.makeSunroof()
# Printing all objects property values
print("Properties of car1")
print("car1 Brand name: ",car1.name)
print("car1 Base Color: ",car1.base_color)
print("car1 seating: ",car1.seating)
print("car1 Sunroof present: ",car1.sunroof)
print("\nProperties of car2")
print("car2 Brand name: ",car2.name)
print("car2 Base Color: ",car2.base_color)
print("car2 seating: ",car2.seating)
print("car2 Sunroof present: ",car2.sunroof)
Properties of car1
car1 Brand name: ABC
car1 Base Color: black
car1 seating: 5
car1 Sunroof present: False
Properties of car2
car2 Brand name: XYZ
car2 Base Color: white
car2 seating: 5
car2 Sunroof present: True
See how only the properties of specific objects are changed without affecting the other objects.
In this tutorial, we have learned about the concepts of classes and objects. If you have any doubt, feel free to ask in the comment section below.
6. References
- Official Documentation
- Ways to create constructors In Python
- Java Class Example Tutorials
- Oops Concepts In Java
Happy Learning 🙂