From Shallow to Deep, Enter Python Decoration - - - Part 2: Advancement of Decoration

Keywords: Python

Last article: From shallow to deep, into Python Decoration - - - Part 1: Foundation

  1. Decorators can be nested

  2. Decorators can be classes or functions; Decorators can modify functions or classes

  3. Types of decorative appliances used:

    1. @ function

      Decorated function

    2. @ function

      Decorative category

    3. @ class

      Decorated function

    4. @ class

      Decorative category

 

  • Nesting of Decorators: Functions Decorated by Lower Decorators are Decorated by Upper Decorators Again

def kuozhan(f):
	def inner():
		print("1 I am an extension function.,Receive new incoming information from outside old :",f)
		f()
		print("4 I am an extension function.,I am new old Function post display")
	return inner
    
def outer(f):
	def inner():
		print("2 I am outer function,Receive external incoming old :",f)
		f()
		print("3 I am outer function,I am here old Function post display")
	return inner


@kuozhan             #old = kuozhan(outer(old))
@outer              #old = outer(old)
def old():
	print("I am old function")
                                                 
old()

>>>1 I am an extension function.,Receive new incoming information from outside old : <function outer.<locals>.inner at 0x000000000250AB70>
>>>2 I came in from outside. old : <function old at 0x000000000250AAE8>
>>>  I am old function
>>>3 I'm a decorator.,I display it after the old function
>>>4 I am an extension function.,I am new old Function post display


@ function

Decorated function


  • Modify the original function with parameters

  • By default, the parameters of old function are passed to the first level function in outer

def outer(f):
	def inner(var):
		print("1 I am outer function,Receive external incoming old :",f)
		print("1 I am outer function,Receive external incoming old Parameters :",var)
		f(var)
		print("3 I am outer function,I am here old Function post display")
	return inner
	
	def inner2(var):
	    pass

@outer                                     #old = outer(old)
def old(var):
	print("2 I am old function","parameter: "var)

var = 'Music'                                                
old(var)

>>>1 I am outer function,Receive external incoming old : <function old at 0x00000000024DAA60>
>>>1 I am outer function,Receive external incoming old Parameters : Music
>>>2 I am old function parameter:  Music
>>>3 I am outer function,I am here old Function post display


  • Modification of primitive functions with multiple parameters

def outer(f):
	def inner(a, b,*args,d,**kwargs):
		print("1 I am outer function,Receive external incoming old :",f)
		print("1 I am outer function,Receive external incoming old Parameters :\
			//General parameter a={}, default parameter b={}, redundant common parameters: {} and {}". format(a,b,args[0],args[1])
		print("1 I am outer function,Receive external incoming old Parameters :\
			//Command keyword parameter d:{}, redundant keyword parameter: {} and {}". format(d,kwargs["key1"],kwargs["key2"])
		f(a,b, *args,d=4,**kwargs)
		print("3 I am outer function,I am here old Function post display")
	return inner


@outer                                     #old = outer(old)
def old(a, b=2, *args,d,**kwargs):         #Five parameters
	print("2 I am old function",
		"General parameter a={0}, Default parameters b={1}, Redundant common parameters:{2}and{3},\
		//Command keyword parameters d:{4}, redundant keyword parameters: {5} and {6}“\
		.format(a,b,args[0],args[1],d,kwargs["key1"],kwargs["key2"]))
dict1 = {"key1":"value1","key2":"value2"}
old(1,22,'args1','args2',d=4,**dict1)

>>> 1 I am outer function,Receive external incoming old : <function old at 0x00000000024AAAE8>
>>> 1 I am outer function,Receive external incoming old Parameters :     General parameter a=1, Default parameters b=22, Redundant common parameters:args1 and args2
>>> 1 I am outer function,Receive external incoming old Parameters :     Named keyword parameters d:4, Redundant keyword parameters:value1 and value2
>>> 2 I am old function General parameter a=1, Default parameters b=22, Redundant common parameters:args1 and args2,Named keyword parameters d:4, Redundant keyword parameters:value1 and value2
>>> 3 I am outer function,I am here old Function post display


Posted by titangf on Sat, 13 Apr 2019 23:06:33 -0700