Python includes the concept of static class data and static class methods. The static class data Here, define a class attribute for the static class data. Explicitly use the class name in the assignment, if you want to assign a new value to the attribute...
Python includes the concept of static class data and static class methods.
Here, define a class attribute for the static class data. Explicitly use the class name in the assignment, if you want to assign a new value to the attribute −
We can also return the following instead return Demo.count −
return self.count
Within a method of Demo, an assignment like self.count = 42 creates a new and unrelated instance named count in self’s own dict. Rebinding of a class-static data name must always specify the class whether inside a method or not −
Demo.count = 314
Let us see how static methods work. A static method is bound to the class and not the object of the class. The statis methods are used to create utility functions.
Static Method cannot access or modify the class state. The Static methods do not know about class state. These methods are used to do some utility tasks by taking some parameters.
Remember, the @staticmethod decorator is used to create a static method as show below −
Let us see a complete example −
Student1 Age = 20 Student2 Age = 21 Student3 Age = 22 Student4 Age = 19 True True