Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python decorator

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use the Python decorator, the editor thinks it is very practical, so share it with you for reference. I hope you can get something after reading this article.

1. Definition and use

Example 1: decorator definition:

Def decorator function (external function):

Def inline function (* args,**kwargs):

... Front decoration.

External function (* args,**kwargs)

... Rear decoration.

Return inline function

Example 2: two call modes of decorator

The first kind: decorator function (external function) (parameter 1, parameter 2.)

The second: bind external functions through @ decorator function names when defined (triggered when external functions are called)

# coding:utf-8:if _ _ name__ = ='_ main__': # example 1 decorator definition # decorator function external function func def decorator (func): # inline function decoration # * args parameter 1, parameter 2. Change to (parameter 1, parameter 2.) # * * kwargs will parameter 3 = parameter value 3, parameter 4 = parameter value 4. Change to {'parameter 3 parameters: parameter value 3, parameter 3, parameter 4, parameter 4, parameter value 4, parameter value 4.} # * args,**kwargs changes parameter 1, parameter 2. Parameter 3 = parameter value 3, parameter 4 = parameter value 4. Change to (parameter 1, parameter 2.), {'parameter 3': parameter 3, 'parameter 4', parameter value 4'.} def inline (* args,**kwargs): # * args,**kwargs will restore the parameter # will (parameter 1, parameter 2.), {'parameter 3: parameter 3 The parameter value of 'parameter 4' is 4 parameters.} becomes parameter 1, parameter 2. Parameter 3 = parameter value 3, parameter 4 = parameter value 4. Name = func (* args, * * kwargs) print (f'name is {name}') # return inline function return inline def talk (name): return name # example 2 two calls to the decorator # the first decorator function (external function) (parameter 1, parameter 2.) Decorator (talk) ('xie') # name is xie # the second @ decorator function name binds the external function @ decorator def see (name): triggers the decorator see (' xie') # name is xie2.@classmethod when return name # is called

1. Class methods decorated with @ classmethod can be done through class. Method (parameter 1, parameter 2.) Call

two。 But self needs to become cls when defining a function

3. The normal method of the class (the method without decorator) cannot be called internally, but the method decorated by @ classmethod,@staticmethod can be called.

4. Access to the properties of a class

5. The method of @ classmethod decoration can be called through self in a normal class.

# coding:utf-8:if _ _ name__ = ='_ _ main__': class A (object): _ _ name = 'python' # Common method def talk (self): print (self.__name) # self.see () methods in ordinary classes that can be decorated by @ classmethod can be called through self # methods decorated by @ classmethod can be accessed through class. Method (parameter 1, parameter 2.) Call # but when defining a function, self needs to become cls @ classmethod def see (cls, description='good'): # cls.talk () Error cannot call the normal method of the class (not @ classmethod @ staticmethod decorated method) # cls.look () can call @ classmethod decorated method # cls.jump () can call @ staticmethod decorated method # can access the class attribute print (f' {cls. _ _ name} is {description}') @ classmethod def look (cls): print (fallowed I) Like {cls.__name}') @ staticmethod def jump (): print (flip I am jump') a = A () a.talk () # python # A.talk () Error cannot pass class. Method (parameter 1, parameter 2.) Call a.see () # python is good # through class. Method (parameter 1, parameter 2.) Call A.see () # python is good

@ staticmethod

1. Class methods decorated with @ staticmethod can be done through class. Method (parameter 1, parameter 2.) Call

two。 But there is no need for self and cls to define functions

3. Because it has no self,cls, it is destined to be unable to access class properties & calling class methods

4. The method decorated with @ staticmethod can be called through self in the ordinary method of the class.

# coding:utf-8:if _ _ name__ = ='_ main__': 'class B (object): _ _ name =' php' def talk (self): # you can call @ staticmethod decoration method self.see (self.__name) # without self via self Cls @ staticmethod def see (description='good'): print (f'description is {description}') B.see () # description is good B.see ('ok') # description is ok B (). Talk () # description is php

@ property

The function decorated by 1.@property is used to replace the same attribute in the class as the function name.

Definition: @ property

Def attribute name (self):

.

two。 Properties replaced by the @ property decorator cannot pass through object. Property name = property value for assignment (unless @ property name .setter decorator is used):

Definition: @ attribute name. Setter

Def attribute name (self, attribute value):

.

3. Functions modified by @ property cannot pass through object. Function name () can only be called by object. Function name as attribute

4. Only properties that have been replaced by @ property can use the @ attribute name. setter decorator

5. The priority of _ _ setattr__ is higher than the priority of @ property name .setter decorator

# coding:utf-8:if _ _ name__ = ='_ main__': 'class A (object): _ _ name =' python' sex = 'man' # cannot be set private # @ property decorated function is used to replace the same attribute in the class as the function name # this replaces the name attribute @ property def name (self): return self.__name

@ property

Def sex (self): return 'woman' # solves the object of the overridden attribute. Attribute = attribute value assignment problem # used with @ property decorator, only attributes replaced by @ property can use @ attribute name. setter decorator @ name.setter def name (self Value): print (f'value is {value}') # _ setattr__ takes precedence over @ attribute name.setter decorator has priority # def _ setattr__ (self, key, value): # print (f'key is {key}) Value is {value}') a = A () print (a.name) # python # print (a.name ()) Error modified by @ property cannot pass object externally. Function name () can only be called by object. The function name is replaced by @ property for print (a.sex) # the function name is replaced by woman, not man # a.sex = 'man' Error, and cannot be replaced by object. Attribute name = attribute value for assignment, unless there is @ attribute name .setter decoration a.name = 'python3.7' # value is python3.7 on how to use Python decorator "this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report