python decorator - module enabling each other

'''

Decorators, the actual operating point of view, is independent of the function, without changing each other, functional overlay, thereby enhancing capacity.

Between modules can energize each other, no longer a simple mutual cooperation / collaboration, goes a step further. Very flexible, and powerful.

The following example shows: when @timer decorative say (), the effect is the function + say timer function, the function execution superposed.

'' '
Import Time
Import nnlog

DEF Timer (FUNC):
  DEF newfunc ():
    log = nnlog.Logger (' zylog.txt ')
    ST = the time.time ()
    FUNC ()
    Runtime = the time.time () - ST
    Print ( '% s of run time was S%'% (FUNC .__ name __, Runtime))
    log.debug ( '% s of run time was S%'% (FUNC .__ name __, Runtime))

  return newfunc



@timer      

'' 'Decorator, when the decorative effect is the function + say timer function, the function execution superposed' ''
DEF say ():
  the time.sleep (2)
  Print ( 'function say !!!')



IF the __name__ == '__main__':
  say ()

Guess you like

Origin www.cnblogs.com/xuexizongjie/p/11013250.html