Python annotator usage learning notes, python learning notes

Source: Internet
Author: User

Python annotator usage learning notes, python learning notes

In python, the @ func. this is the decorator. The decorator uses a function as a parameter and is often used to extend existing functions, that is, adding a function without changing the current function status.

def run():  print "I'm run."

I have such a function. I want to know when and when this function will end. I should write it like this.

def run():  print time.ctime()  print "I'm run."  print time.ctime()

However, if the function cannot be modified, you need a decorator.

def count(func):  def wrapper():    print time.ctime()    ret = func()    print time.ctime()    return ret  return wrapper@countdef run():  print "I'm run."      # print '2015-4-10'

Eg:

def now():  print '2015-4-10'f = nowf()  

 

A function has a _ name _ object which can be defined by dir (func) func.

now.__name__    # print 'now'f.__name__     # print 'now'print f       # print '<function now at 0x000000000213A908>'print now      # print '<function now at 0x000000000213A908>'

We use the decorator to print log logs.

def log(func):  def wrapper(*args, **kwargs):    print "call %s()" % func.__name__    return func(*args, **kwargs)  return wrapper@logdef now():  print '2015-4-10'now()        # print 'call now()'

In fact, the modifier function is equivalent to now = log (now), that is, the modifier function assigns the modified function as a parameter to a variable of the same name.

Functools. wraps Function

When the decorator is used, the value of now _ name _ has changed.

# The previous now. _ name _ # print 'now '# now. _ name _ # print 'wrapper' is not used'

Before using the decorator, now. _ name _ uses the current now function, but after use, the now function is actually a log (now), that is, the return value of the log function, that is, the wrapped wrapper. the solution is functools. wraps function.

Decorative closure,Call import functools before use

def log(func):  @functools.wraps(func)  def wrapper(*args, **kwargs):    ...

Decorator with Parameters

If the decorator needs to input parameters, you need to write a higher-order function that returns the decorator, which is more complex to write.

Def login (level): def _ deco (func): def wrapper (* args, ** kwargs): if level> = 5: print 'user VIP grade % d' % int (level-5) else: print 'user silk grade % d' % abs (level-5) return func (* args, ** kwargs) return wrapper return _ deco @ login (5) def user (username): print 'Welcome, % s' % username # user vip level 0 # welcome, minkuser ('mink ')

Decorator with parameters is equal to func = decorator function (decorator parameter) (func)

Decorations

You can use classes like functions through the _ call _ of classes.

class A(object):  def __init__(self, func):    self.func = func  def __call__(self):    return self.func() ** 2@Adef foo():  return 10print foo()   # print 100

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.