Class Methods in OOPs In Python
class Person:
instances = 0
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
Person.instances += 1
@classmethod
def inst_no(cls):
print(f"{cls.__name__} class has {cls.instances} instances.")
p1 = Person("Adrika", "Bhadauria")
p2 = Person("Anika", "Singh")
Person.inst_no()
Post a Comment