Python Data Persistence – Class Level Attributes and Methods
In the above example, MyClass defines two data variables __myname and __myage that are instance attributes. They are invariably initialized through __init__( ) constructor. Their values are different for each object. A class however may have an attribute such that its value is the same for all existing objects. In other words, such attribute is a shared or common resource and defined outside the __init__( ) method, for that matter outside any instance method.
In the following script, the total is a class variable of a player class. It is defined with the purpose of maintaining a running sum of runs scored by each player. The player class defines a name and runs as instance attributes initialized through __init__( ) method as usual which also keeps on adding runs of each object.
- Python Program to Form a Dictionary from an Object of a Class
- Python Data Persistence – class Keyword
- How to Get a Dictionary from an Object’s Fields in Python?
#classattr.py class player: __total=0 def__init__(self, name, runs): self.__name=name self.___runs=runs player.__total+=self.__runs print ('Total runs so far:',player.___total)
Let us import the player class and set up a few objects. Following interpreter, activity shows that the ___total variable is being cumulatively updated by ___runs of each object.
>>> from classattr import player >>> p1=player('Virat', 60) Total runs so far: 60 >>> p2=player('Rahul', 45) Total runs so far: 105
Two things are to be noted here. First, the use of the += operator. It is an in¬place addition operator effectively assigning the addition of two operands back to the left operand. Hence player. total==+=self. runs actually becomes player. total=player. total+self.runs . In-place variations of other operators defined in Python are +=, -=, *=, /=, and so on.
Secondly, the value of __total is retrieved with the help of the name of the class (player.__total) rather than self. This is obvious because the total is a class variable and not an instance variable specific to any particular object.
In view of this feature, Python has a provision to define methods that can access such class attributes. A class method needs the name of the class to be passed to it as an argument (conventionally using ‘cls’ identifier). The class can also have a static method that doesn’t need an explicit reference to either class or object which means there’s no argument to it in the form of self or els.
Class method and static method is decorated by built-in @classmethod and @statiemethod directives.
#classattr.py class player: ___total = 0 def__init__(self, name, runs): self.___name=name self.___runs=runs player.___total+=self.___runs print ('Total runs so far:',player.___total) @classmethod def printtotal(cls): print ('Total runs so far:',els.___total) @staticmethod def displaytotal( ): print ('Total runs so far:',player.__total)
>>> from classattr import player >>> p1=player('Virat',60) Total runs so far: 60 >>> p2=player('Rahul',45) Total runs so far: 105 >>> player.printtotal( ) Total runs so far: 105 >>> player.displaytotal( ) Total runs so far: 105