Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the use of class methods and static methods in python

Shulou Source: shulou.com Published: 2022-06-03 15:34:00 09月16日 Update

This article mainly introduces the use of class methods and static methods in python. It is very detailed and has a certain reference value. Friends who are interested must read it!

1. Class methods, mainly use class methods to manage class attributes, whether private attributes or general attributes.

In many classes, instantiation may not be required, just for wrapping purposes. At this point, we can manage class properties through class methods.

2, static method, when we have a lot of cluttered unrelated functions, we need to wrap the function in the class, but can not modify the function code, that is, parameters.

Just to wrap these functions through classes for ease of management.

Example

# here put the import lib class Tool (object): "defines a toolkit to manage these tools, just to encapsulate without affecting these functions It is convenient to manage "" @ staticmethod def hammer (): # Hammer pass @ staticmethod def ax (): # axe pass @ staticmethod def wrench (): # wrench pass # when we need to use these tools, we can call them directly with the class object # without passing in additional parameters, such as self- instance itself, cls-class itself Just to achieve the effect of encapsulation Tool.hammer () Tool.ax () above is all the content of the article "what are class methods and static methods in python?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Methods functions management properties static instances tools content parameters just articles wrappers wrappers normal messy code values interests can be passed at the same time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Apple Linux macOS Docker