Python: Class and Object [OOPs]

A class defines the blueprint, which can be instantiated to create Object(s)
Python provides a special member function called __init__ which is called every time we create an instance of a class and this function is used to create the blueprint of the object.
Sometimes __init__ is also called a constructor function.
The function __init__ is called whenever an object is created. The function takes one mandatory parameter which is called self. Here self refers to the object itself.
self is not a keyword in Python, it’s just a norm to use the term self as a reference to itself, however, we can name it anything we want, for example
inside this __init__ function, we create local instance variables which define the state (identifiable characteristics) of the object built out of this class.
class Student:
def __init__(self):
self.name = "ABC"
self.age = 25obj1 = Student()
print(obj1)
print(obj1.name)
print(obj1.age)
print(obj1.__dict__)
Output:
<__main__.Student instance at 0x7fb882a0b6c8>
ABC
25
{'age': 25, 'name': 'ABC'}Adding Functions to the class:
The functions in the class provide the behavioural aspect of the object.Except __init__ all other functions are called member functions of the class.
class Student:
def __init__(self,name,age):
self.name = name
self.age = agedef student_info(self):
return print(f"Name is:{self.name} with age {self.age}")obj1 = Student('lokesh',26)
obj1.student_info()
Output:
Name is:lokesh with age 26Just like __init__ function, all functions take self as a mandatory parameter.
Creating Instance variables in Multiple functions:
Those instance variables will be created only when the function creating the instance variable is called.
class Student:
def __init__(self,name,age):
self.name = name
self.age = agedef student_info(self):
return print(f"Name is:{self.name} with age {self.age}")
def parents_info(self,pname):
self.pname=pname
return print(f"Parent Name is:{self.pname}")obj1 = Student('lokesh',26)
obj1.student_info()
obj1.parents_info('vinod')
Output:
Name is:lokesh with age 26
Parent Name is:vinod sharmaclass variables:
A class variable is one which is declared inside the class, but not inside any method(s) of the class.
class Student:
country='India' #class Variable
def __init__(self,name,age):
self.name = name
self.age = agedef student_info(self):
return print(f"Name is:{self.name} with age {self.age}")obj1 = Student('lokesh',26)
obj1.student_info()
print(obj1.__dict__)
print(Student.__dict__)
print(obj1.country)
Output:
Name is:lokesh with age 26
{'name': 'lokesh', 'age': 26}
{ 'country': 'India', ...with many oter things}
Indiain above if we print dictionary of obj1 than country is not visible there for this we print dictionary of class Student.
It’s the python lookup sequence which first looks at the “Instance”, and if variable / function is not found, it looks at the “Class”. This is the reason we can print obj1.country using instance object.
Class Methods:
A class method is a special method which takes class as parameter instead of instance i.e. self . While defining class method, we need to explicitly tell python that the method is a class method using the keyword @classmethod
class Student:
country='India' #class Variable def __init__(self,name,age):
self.name = name
self.age = age @classmethod
def newcountry(cls,newcountry):
cls.country=newcountry
We can now call this new method newcountry via Instance or via Class. In both the cases, the end result will be the same. Here is how we call the method using Instance.
class Student:
country='India' #class Variable def __init__(self,name,age):
self.name = name
self.age = age @classmethod
def newcountry(cls,newcount):
cls.country=newcount
obj1 = Student('lokesh',26)
print(obj1.country)# Call using Instance
obj1.newcountry('Bharat')obj2 = Student('lokesh',26)print(obj2.country)
print(obj1.country)
Output:
India
Bharat
BharatAnd we can call using class as
def __init__(self,name,age):
self.name = name
self.age = age@classmethod
def newcountry(cls,newcount):
cls.country=newcount
obj1 = Student('lokesh',26)
print(obj1.country)# Call using class
Student.newcountry('Bharat')obj2 = Student('lokesh',26)print(obj2.country)
print(obj1.country)
Output:
India
Bharat
Bharat