The Python static method, class method article has used the method of generating functions outside of a class to calculate the number of instances of the class. This article explores the use of static and class methods to implement this functionality. A statistical instance using static methods
Example 1.static.py
# -*-coding:utf-8-*- class Spam: = 0 def__init__(self): + = 1 def Printnuminstance (): print'number ofinstance:', Spam.numinstance = Staticmethod (printnuminstance) # Use the built-in function to staticmethod a function into a static function
Call Result:
from Import Spam # Import Class >>> A, b, C = Spam (), Spam (), Spam () # instantiation >>> Spam.printnuminstance () # class calls number of instance:3>>> a.printnuminstance () # instance is called, the instance parameter is not passed in Number of Instance:3
Static method calls compare to methods that move pirntnuminstance outside the class, with the following advantages:
1. The static method changes the function name into a local variable within the scope of the class so that the function name does not conflict with other variable names within the module.
2. Move the function program code to its near-use location for easy searching.
3. Allow subclasses to customize static methods. As shown in Example 2.
Example 2.custom_made.py
class Sub (Spam): # sub-class sub def printnuminstance (): # redefine Print ' Extra Stuff ... ' spam.printnuminstance () = Staticmethod (printnuminstance)
Call Result:
>>>a, B = Sub (), Sub () # instantiation >>> a.printnuminstance () # instance Call 2 # Restart the interpreter to make the call. Otherwise it should be 5 because there are already three instances of the # class call 2
In addition, classes can inherit methods without being redefined, as follows:
class Other (Spam): # full inheritance of static methods ... Pass ... >>> C = Other ()>>>3
II. statistical examples using class methods
Example 3:a_class.py
# -*-coding:utf-8-*- class Spam: = 0 def__init__(self): + = 1 def printnuminstance (CLS ): # receives the instance's (Bottommost) class as a parameter, passing in the CLS in the print'number ofinstance: ', cls.numinstance = Classmethod (printnuminstance)
Call Result:
from Import # Import Module >>> A, B = Spam (), Spam () # instantiation >>> a.printnuminstance () # instance invocation, the lowest class of the incoming instance A is Spamnumber of instance:2>>> spam.printnuminstance () # class invocation, passing in class spam Number of Instance:2
Three summary
Because class methods always receive the lowest class of an instantiated class:
1. Static method: Suitable for handling local data of a class
2. Class method: For handling data for each class in the class hierarchy
The following example (example 4) shows the use of class methods to manage instance counters for each class. In the code in Example 4, the top-level superclass uses a class method to manage state information, which differs depending on the class in the tree and is stored on the class.
Example 4:test.py
class spam:numinstance = def count (CLS): Cls.numinstance + = 1 def __init__ = Classmethod (Coun T) class Sub (Spam): numinstance =< Span style= "COLOR: #000000" > 0 def __init__ __init__ () class Other (Spam): numinstance = 0
Call Result:
>>>x = Spam ()>>>y1, y2 = Sub (), Sub ()>>>z1, z2, z3 = other (), Other (), Ot Her ()>>>x.numinstance, Y1.numinstance, Z1.numinstance1, 2, 3>>> Spam.numinstance, Sub.numinstance, Other.numinstance1, 2, 3
Python static Methods, class methods (ii)