What are Classes and Objects in Python
In Python, Classes and Objects are fundamental concepts in object oriented programming (OOP). With the help of Classes and Objects we can model real-world entities and their behaviors in the code. Here is what classes and objects are:
A Class is a blueprint or template for creating objects. In python, classes are created using the class keyword. Here is a syntax:
class ClassName: x= "Hello Python Programmers"
In an above code we first created a class namely ClassName, then we define a class attribute named x and assign it to a string “Hello Python Programmers”. (Class attribute are actually variables that belong to the class itself.)
Now we can create an object using a class ClassName and print the value of x.
Creating a Python Object
So to create an object in Python, we have to use a class name that we have previously created. In an above example we have created a class namely ClassName, now we will use this ClassName to create an object. Here is an Example:
class ClassName: x= "Hello Python Programmers" obj = ClassNmae() print(obj.x)
After creating a class and it’s attribute we have then created an instance of the class “ClassName” (Object) and stores it in a variable obj. At last we use the print function to print the value of the attribute
x of the instance
obj. Since the instance was created from the
ClassName class, it inherits the class attribute
obj.x prints “Hello Python Programmers”.
Python Constructor – (__init__ Function)
The __init__ function is a special method in Python Classes. It gets called when an instance of a class (object) is created. It is used to initialize the attributes (data members) of an object. We always have to add
self parameter in the
__init__ function. It refers to the instance being created, and we can use it to set initial values for the object’s attributes.
Here is an example of the __init__ function.
class Person: def __init__(self, name, age): self.name = name self.age = age # Creating an instance of the class obj = Person("John", 19) print(obj.name) print(obj.age)
In an above code, we first created a class namely Person, which has two attributes name and age. The
__init__ constructor of the person class is a special method that gets called when an instance of the class (object) is created. The
__init__ constructor takes three parameters self ( refers to the instance being created.), name and age.
self.age assign the values of the name and age parameters to the corresponding attributes of the instance. The
self keyword refers to the instance being created and allows you to access its attributes.
After that we have created an instance of a class Person (Object), and store it in a variable obj. The arguments
19 are passed to the constructor to set the
age attributes of an object. At last we print the values of the
age attributes of the
obj instance, Using the
print(obj.age)function. The output we get is John and 19.
Python self Parameter
self parameter in python refers to the current instance of the class and is used to access the instance’s attributes and methods. It’s not a reserved keyword, but it’s widely used and expected by convention.
Whenever we define a method within a class, the first parameter should always be
self. For example:
class MyClass: def __init__(self, par1, par2): # Method code here
Share This Post!