Variables that are declared within a class declaration and outside methods are called class variables. Class variables belong to a class and not to instances of a class. They are shared among all the objects of a class. That is why class variables are also known as static variables.
In programming languages such as Java and C++, static
keyword is used to declare static variables whereas, in Python, there is no static
keyword.
Class variables can be accessed from within the class as well as from outside the class.
class Student: noOfStudents = 0 def __init__(self, firstname, lastname): self.firstname = firstname self.lastname = lastname Student.noOfStudents += 1 def show(self): print('Student Name:', self.firstname, self.lastname) print('Total Number of students:', type(self).noOfStudents) s1 = Student('Geeta', 'Sahu') s1.show() s2 = Student('Kaushal', 'Sahu') s2.show()
Output
Student Name: Geeta Sahu Total Number of students: 1 Student Name: Kaushal Sahu Total Number of students: 2
As you can see above, noOfStudents
is a class variable because it is defined within the class declaration and outside of methods. In this way, you can declare class variables in a class.
Instance, class and static method can access class variables. Each method has its own way of accessing static variables.
Methods | Ways of accessing class variables |
---|---|
Instance Method | From inside instance method, you can access class variables using the following two ways-
|
Class Method | There are two ways of accessing class variables from inside class method-
|
Static Method | There is only one way to access class variables from static method-ClassName.ClassVariable
|
#Python Program to test accessibility of class variables inside a class class Person: noOfPersons = 0 def display(self): print('Inside instance method') type(self).noOfPersons = type(self).noOfPersons + 1 print('No of persons: ', type(self).noOfPersons) Person.noOfPersons = Person.noOfPersons + 1 print('No of persons: ', type(self).noOfPersons) @classmethod def show(cls): print('Inside class method') cls.noOfPersons = cls.noOfPersons + 1 print('No of persons: ', cls.noOfPersons) Person.noOfPersons = Person.noOfPersons + 1 print('No of persons: ', Person.noOfPersons) @staticmethod def appear(): print('Inside static method') Person.noOfPersons = Person.noOfPersons + 1 print('No of persons: ', Person.noOfPersons) p = Person() p.display() p.show() p.appear()
Output
Inside instance method No of persons: 1 No of persons: 2 Inside class method No of persons: 3 No of persons: 4 Inside static method No of persons: 5
Outside a class, you can access class variables in two ways-
obj.ClassVariable
)ClassName.ClassVariable
)Remember one thing, when you modify class variables using object then you end up creating an instance variable having the same name as that of a class variable for that object, that is why it is recommended to access class variables using class name instead of object. This might seems to be confusing so it's better to understand this concept with the help of an example-
#Python Program to test accessibility of class variables outside a class class Person: noOfPersons = 0 def __init__(self): type(self).noOfPersons = type(self).noOfPersons + 1 p1 = Person() p2 = Person() print('Accessing class variable using object p1: ', p1.noOfPersons) print('Accessing class variable using object p2: ', p2.noOfPersons) print('Accessing class variable using classname: ', Person.noOfPersons) p1.noOfPersons = 12 Person.noOfPersons = 3 print('Accessing class variable using object p1: ', p1.noOfPersons) print('Accessing class variable using object p2: ', p2.noOfPersons) print('Accessing class variable using classname: ', Person.noOfPersons)
Output
Accessing class variable using object p1: 2 Accessing class variable using object p2: 2 Accessing class variable using classname: 2 Accessing class variable using object p1: 12 Accessing class variable using object p2: 3 Accessing class variable using classname: 3
Before the modification of class variable (noOfPersons
) using object, the value of noOfPersons
is same for everyone. But when object p1
modified its value then class variable transforms to instance variable for object p1.