程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Decorator for Python

編輯:Python

Define and use

# coding:utf-8:
if __name__ == '__main__':
''' example 1: Decorator definition : def Decorator function ( External function ): def Inline function (*args,**kwargs): ... Front decoration ... External function (*args,**kwargs) ... Rear trim ... return Inline function example 2: Decorator can be called in two ways The first one is : Decorator function ( External function )( Parameters 1, Parameters 2......) The second kind : Defined by @ Decorator function name binding External function Triggered when an external function is called '''
# example 1 Decorator definition 
# Decorator function External function func
def decorator(func):
# Inline function Decorate 
# *args take Parameters 1, Parameters 2...... Turn into ( Parameters 1, Parameters 2.......)
# **kwargs take Parameters 3= Parameter values 3, Parameters 4= Parameter values 4...... Turn into {' Parameters 3': Parameter values 3,' Parameters 4':' Parameter values 4'......}
# *args,**kwargs take Parameters 1, Parameters 2...... Parameters 3= Parameter values 3, Parameters 4= Parameter values 4...... Turn into ( Parameters 1, Parameters 2.......),{' Parameters 3': Parameter values 3,' Parameters 4':' Parameter values 4'......}
def inline(*args, **kwargs):
# *args,**kwargs Restore parameters 
# take ( Parameters 1, Parameters 2.......),{' Parameters 3': Parameter values 3,' Parameters 4':' Parameter values 4'......} Turn into Parameters 1, Parameters 2...... Parameters 3= Parameter values 3, Parameters 4= Parameter values 4......
name = func(*args, **kwargs)
print(f'name is {
name}')
# return Inline function 
return inline
def talk(name):
return name
# example 2 Two ways to call decorators 
# The first one is Decorator function ( External function )( Parameters 1, Parameters 2......)
decorator(talk)('xie') # name is xie
# The second kind @ Decorator function name binding External function 
@decorator
def see(name):
return name
# Trigger decorator when called 
see('xie') # name is xie

@classmethod

# coding:utf-8:
if __name__ == '__main__':
''' 1. By @classmethod Decorated class methods can be implemented through class. Method ( Parameters 1, Parameters 2......) call 2. But when defining functions self Need to become cls 3. It cannot call ordinary methods of a class ( A method of decorating without ornaments ), You can call @classmethod,@staticmethod Method of decoration 4. Can access class properties 5. Common classes can pass self call @classmethod Method of decoration '''
class A(object):
__name = 'python'
# Common method 
def talk(self):
print(self.__name)
# self.see() Common classes can pass self call @classmethod Method of decoration 
# By @classmethod Decorated class methods can be implemented through class. Method ( Parameters 1, Parameters 2......) call 
# But when defining functions self Need to become cls
@classmethod
def see(cls, description='good'):
# cls.talk() Error You cannot call ordinary methods of a class ( Not @classmethod,@staticmethod The method of decoration )
# cls.look() You can call @classmethod Method of decoration 
# cls.jump() You can call @staticmethod Method of decoration 
# Can access class properties 
print(f'{
cls .__name} is {
description}')
@classmethod
def look(cls):
print(f'I like {
cls.__name}')
@staticmethod
def jump():
print(f'I am jump')
a = A()
a.talk() # python
# A.talk() Error Cannot pass class. Method ( Parameters 1, Parameters 2......) call 
a.see() # python is good
# adopt class. Method ( Parameters 1, Parameters 2......) call 
A.see() # python is good

@staticmethod

# coding:utf-8:
if __name__ == '__main__':
''' 1. By @staticmethod Decorated class methods can be implemented through class. Method ( Parameters 1, Parameters 2......) call 2. But when defining functions need not self and cls 3. Because it has no self,cls It is doomed that it cannot access class properties & Calling class methods 4. In the ordinary method of a class, you can use self call @staticmethod Method of decoration '''
class B(object):
__name = 'php'
def talk(self):
# Can pass self call @staticmethod Method of decoration 
self.see(self.__name)
# need not 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

# coding:utf-8:
if __name__ == '__main__':
''' [email protected] The decorated function is used to replace the attribute with the same function name in the class Definition : @property def Property name (self): ....... 2. By @property Properties replaced by decorators , Unable to get object. Property name = Attribute value ( Unless you use @ Property name .setter Decorator ): Definition : @ Property name .setter def Property name (self, Property value ): ...... 3. By @property Decorated functions cannot be passed externally object. Function name () call , Can only object. Function name As attribute 4. Only by @property Can be used instead of the attribute of @ Property name .setter Decorator 5. __setattr__ Has a higher priority than @ Property name .setter Decorator priority '''
class A(object):
__name = 'python'
sex = 'man'
# Cannot be set to private 
# @property The decorated function is used to replace the attribute with the same function name in the class 
# This replaces name attribute 
@property
def name(self):
return self.__name
@property
def sex(self):
return 'woman'
# Solve the problem of object. attribute = Property value Assignment problem 
# coordination @property Decorator use , Only by @property Can be used instead of the attribute of @ Property name .setter Decorator 
@name.setter
def name(self, value):
print(f'value is {
value}')
# __setattr__ Has a higher priority than @ Property name .setter Decorator priority 
# def __setattr__(self, key, value):
# print(f'key is {key}, value is {value}')
a = A()
print(a.name) # python
# print(a.name()) Error By @property Decorated functions cannot be passed externally object. Function name () call , Can only object. Function name As attribute 
# By @property Instead of 
print(a.sex) # yes woman No man
# a.sex = 'man' Error Replaced attribute , Cannot pass object. Property name = Property value Assign a value , Unless there is @ Property name .setter decorate 
a.name = 'python3.7' # value is python3.7

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved