Instance method in python | Explain method in python | what is instance method in oop using python |
Instance Method :-
1. Instance method takes one parameter self which points to an instance of class.
2. When method is called through self parameter ,instance method can freely access attributes and other method on same object.
3. In Class Method receive cls as implicate first argument just like an instance method receive instance i.e self as implicit first argument
4. instance method require creating an object/ instance for accessing methods and properties . this give them a lot of power when it comes to modifying an object state .
Program Using Instance Method -
Output -
Instance method |
Program Using Instance Method -
class person:
total_object = 0
def __init__(self):
person.total_object=person.total_object+1
def showcount(self): # instance method
print("Total objects: ",self.total_object)
p1=person()# creating an object
p2=person()# creating an object
p1.showcount() #call instance method using object.
total_object = 0
def __init__(self):
person.total_object=person.total_object+1
def showcount(self): # instance method
print("Total objects: ",self.total_object)
p1=person()# creating an object
p2=person()# creating an object
p1.showcount() #call instance method using object.
Output -
Total objects: 2
Post a Comment