Quantitative trading in python basics-class attributes, class methods, static methods

class Tool(object): 
    count = 0   # Class attribute
 
    def __init__(self, name="Tool"): 
        self.name 
        = name Tool.count += 1   # Every time a new object is created, the value of the class attribute +1 

    # Class method 
    @classmethod 
    def show_tool_count(cls): 
        print(cls.count) # Static method (do not access the properties of the class or object) 
    @staticmethod 
    def some_method(): 
        print("some method") 
tool1 = Tool("AX") 
tool2 = Tool("hammer") 
tool3 = Tool("bucket") 
Tool.some_method() # directly use the class name to call the static method 
print(Tool.count) # 3 
print(tool1.count) # 3 
print(tool2.count ) # 3 
print(tool3.count) # 3 
Tool.count = 5

    





print(Tool.count) # 5
print(tool1.count) # 5 

tool1.count = 9 
print(Tool.count) # 5, directly modify the class attribute through the object, the value of the class attribute does not change 
print(tool1.count) # 9

Guess you like

Origin blog.csdn.net/Michael_234198652/article/details/109155957