Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use @ Staticmethod, @ Classmethod, and @ property class decorators

Shulou Source: shulou.com Published: 2022-06-03 01:04:42 10月26日 Update

This article mainly explains "how to use @ Staticmethod, @ Classmethod and @ property class decorators". The explanation in this article is simple and clear and easy to learn and understand. please follow the editor's train of thought to study and learn "how to use @ Staticmethod, @ Classmethod and @ property class decorators".

@ staticmethod and @ classmethod are called decorators in Python and are used to decorate a function, which is equivalent to adding an extra function and is no longer instantiated like a normal function.

@ staticmethod and @ classmethod

In fact, both @ staticmethod and @ classmethod are used to declare static methods. It's just a declared static method, a declared class method.

Static method: use the decorator @ staticmethod. There are no self and cls parameters.

Class method: use the decorator @ classmethod. The first parameter is the object of the current class, usually cls.

To make it easier for you to understand the difference, the following sample code will help you find the difference:

'' @ Author:Runsen @ Wechat official account: King of Python @ blog: https://blog.csdn.net/weixin_44510615 @ Date:2020/8/30''class A (): # class attribute x = 1 @ classmethod def get_name (cls Name): print (cls.x) print ('my name is% s'% name) @ staticmethod def get_age (age): print (A.x) print (Flemi am% s years old'% age) if _ _ name__ = ='_ main__': A.get_name ('Runsen') A.get_age (20) # The instance object also runs a = A () a.get_name ('Runsen') a.get_age (20) # output as follows: 1 my name is Runsen 1 I am 20 years old 1 my name is Runsen 1 I am 20 years old

If you want to call some property methods of this class in @ staticmethod, you can only call the class name directly. Property name or class name. Method name. Since @ classmethod holds the cls parameter, you can call the properties of the class, the methods of the class, the instantiated objects, etc., through cls to avoid hard coding.

@ property

@ property can turn a method call into a property call. Example: usually we call data properties and methods, like this

Class School (): name = "go to college at home" def test (self): print ("instance method") @ property def test_pro (self): print ("static attribute") if _ _ name__ = = "_ _ main__": s = School () print (s.name) s.test () # output the following example method for home college

The @ preperty decorator, which turns a method into a property call, is used.

Class School (): name = "go to college at home" def test (self): print ("instance method") @ property def test_pro (self): print ("static property") if _ _ name__ = = "_ _ main__": s = School () print (s.name) # Don't add () s to the returned function. The test_pro # output is as follows: home college static properties

From today on, @ property is widely used in the definition of Python classes in Python programming. As soon as you see @ property, your first reaction is: @ property is responsible for making a method an attribute of the same name.

Thank you for reading, the above is the content of "how to use @ Staticmethod, @ Classmethod and @ property class decorators". After the study of this article, I believe you have a deeper understanding of how to use @ Staticmethod, @ Classmethod and @ property class decorators, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Methods properties instances static universities functions parameters objects learning output content that is differences class names general code public functions blogs just Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Apple Xiaomi NVidia Shulou Information