Python Classes can have instance variables and Class Variables. Instance variables are bound to instance of the Object Only, Where as Class Variables all objects of the class share same meory location. i.e Instance variables has different memory locations for each object, Where as class variables has single memory location for all Objects of that class.
class Employee: count=0; # class variable/attribute def __init__(self,name,salary): self.name=name self.salary=salary Employee.count+=1 #class variable incremented by 1 @staticmethod def display(): print("Number of Employees ",Employee.count) #prints count of the Employees e1=Employee("abc",774748) e2=Employee("aa",8383883) Employee.display() e2=Employee("jhk",788838) Employee.display() ----------------------Output---------------------------- Number of Employees 2 Number of Employees 3
The above class Employee has class variable/attribute count, which is associated with Class itself,rather than Object.
i.e <className>.<class variable> Class variables count are accessed with Class name Employee
Employee count is incremented everytime new employee object is created.
Constructor __init__ calls Employee.count += 1 , has number of Employee Objects are created
A Method without self parameter or a method decorated with @staticmethod are called as static methods. A static method is related to the class itself where it is defined. It cannot be accessed with Instance Objects, It can be accessed with Class Name only.
<<ClassName>>.<<staticMethod>>