Python parametric Adorner

Source: Internet
Author: User

When parsing the adorner in the code, Python passes the decorated function as the first parameter to the adorner function. If you want the adorner to accept other parameters, you need to create an adorner factory function, pass the argument to it, return an adorner, and then apply it to the function you want to decorate.

The simplest adorner is the register in Example 1:

(Example 1)

#BEGIN registration_abridgedRegistry = []defRegister (func):Print('running Register (%s)'%func) Registry.append (func)returnFunc@registerdefF1 ():Print('running F1 ()')Print('running Main ()')Print('Registry', Registry) F1 ()#END registration_abridged

To facilitate the startup and disabling of the function registration function of register execution, we can provide it with an optional active parameter, which is set to false when the decorated function is not registered. Implementations such as Example 2:

(Conceptually, this new register function is not an adorner, but an adorner factory function.) Calling it will return a true adorner, which is the adorner applied to the target function.

(Example 2)

# BEGIN registration_paramregistry = set () # <1>  def Register (active=true): # <2>  def decorate (              Func): # <3>  print (' Running register (active=%s)->decorate (%s) '         % (active, func)) If active: # <4>  Registry.add (func)  Else:registry.discard (func) # <5>  return func # <6>,  return decorate # <7>  @register (A Ctive=false) # <8>  def f1 (): print (' Running F1 () ') @register () # <9>  def f2 (): Print (' Running F2 () ') def f3 (): Print (' Running F3 () ') # END REGISTRATION_PA RAM 

< 1 > Registry is now a set object, so adding and removing functions is faster.

< 2 > Register accepts an optional keyword parameter.

< 3 > Decorate this intrinsic function is a true adorner, and its parameters are a function.

< 4 > Register func only if the value of the active parameter (obtained from the closure) is true.

< 5 > If active is not true and Func is in the register, delete it.

< 6 > Decorate is an adorner and must return a function.

< 7 > Register is the adorner factory function and therefore returns decorate.

< 8 > @register the factory function must be called as a function and pass in the required parameters.

< 9 > Even if the parameter is not passed in, the register must be called as a function (@register), that is, to return the true adorner decorate.

The key here is that register () returns decorate and then applies it to the decorated function.

The code in Example 2 is in the registeration_param.py module. After importing, the results are as follows:

>>>import registration_paramrunning Register (active=false)->decorate (<function F1 at 0x03320F18>) Running register (active=true)->decorate (<function F2 at 0x03320ed0>)

{<function F2 at 0x03320ed0>}

The F2 function here is in registry, but F1 is not in it because the parameter passed to the Register adorner factory function is Active=false, so F1 applied to decorate did not add it to the register.

If you do not use the @ syntax, register is used as a regular function, and if you want to add F to the Register, the syntax of the decorated F function is register () (f); If you do not want to add or delete it, the syntax is register (active= False) (f). Example 3 shows how to add a function to the register, and how to remove a function from it.

Example 3

>>>from registration_param import*>>>registry                                #<1>{<function F2 at 0x03320ed0>} >>>register () (F3)                        #<2>running Register (active=true)->decorate ( <function f3 at 0x03320f60>) <function F3 at 0x03320f60> >>>registry #<3>                                {< function F2 at 0x03320ed0>, <function f3 at 0x03320f60>} >>>register (Active=false) (F2)     #< 4>Running Register (active=false)->decorate (<function F2 at 0x03320ed0>) <function F2 at 0x03320ed0 >  >>>registry                                #<5>{<function f3 at 0x03320f60>}

< 1 > When importing this module, F2 is in registry.

The < 2 > register () expression returns decorate and then applies it to the F3.

< 3 > The previous line adds the F3 to the registry.

< 4 > This call removes F2 from registry.

< 5 > Confirm registry.

The principle of parameterized adorners is very complex, and parametric decorators usually replace the decorated function, and the structure requires more than one layer of nesting.

Python parametric Adorner

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.