Clever usage of @ classmethod of class_static in python

  • 2021-11-13 02:14:21
  • OfStack

The use of @ classmethod of class_static in python, the use of classmethod, It is mainly aimed at classes rather than objects, When you define a class, you often define some static private attributes. However, when using classes, the private attributes of classes may be modified, but before using class method, the attributes of classes can only be modified through objects, which will lead to a problem. When many objects use this attribute, do we need to modify objects one by one? The answer is that there will be no such mindless program, which leads to the wonderful use of classmethod. Look at the following code:


class Goods:
    __discount = 0.8
    def __init__(self,name,money):
        self.__name = name
        self.__money = money
    @property
    def price(self):
        return self.__money*Goods.__discount
    @classmethod
    def change(cls,new_discount):# Notice that it's not here self It's, but cls Make a replacement 
        cls.__discount = new_discount

apple = Goods(' Apple ',5)
print(apple.price)
Goods.change(0.5) # This is not the use of apple.change() Modified 
print(apple.price)

The above is just a simple list of class method usage scenarios, if there is a new will continue to update this article 2. Since @ staticmethod and @ classmethod can be called directly class name. Method name (), what is the difference between them

Judging from their use,
@ staticmethod does not require self that represents its own object and cls that represents its own class, just like using Function 1.
The @ classmethod also does not require an self parameter, but the first parameter needs to be an cls parameter representing its own class.

If you want to call some attribute methods of this class in @ staticmethod, you can only call the class name. Attribute name or class name. Method name directly.
Because @ classmethod holds cls parameters, it can call class properties, class methods, instantiate objects, etc., avoiding hard coding.
The code below.


class A(object):  
    bar = 1  
    def foo(self):  
        print 'foo'  

    @staticmethod  
    def static_foo():  
        print 'static_foo'  
        print A.bar  

    @classmethod  
    def class_foo(cls):  
        print 'class_foo'  
        print cls.bar  
        cls().foo()  
### Execute   
A.static_foo()  
A.class_foo() 

Extension of Knowledge Points: Usage of python classmethod

Requirement: Add class object attributes and use this variable when creating new concrete objects


class A():
 
    def __init__(self,name):
        self.name = name
        self.config = {'batch_size':A.bs}
    @classmethod
    def set_bs(cls,bs):
        cls.bs = bs
    def print_config(self):
        print (self.config)
 
A.set_bs(4)
a = A('test')
a.print_config()

The above is class_static in python @ classmethod use details, more information about python classmethod use please pay attention to other related articles on this site!


Related articles: