how can i use '@' by myself function like '@staticmethod'
the next is my code,it can print 'xxx'
, but run wrong at last:
def a(object):
print 'xxx'
@a
def b():
return 'bbb'
b()
In your answers, please try to use code examples rather than text, because my Engl开发者_JAVA百科ish is not very good. Thank you.
The decorator form @a
means:
@a
def b...
is exactly the same as:
def b...
b = a(b)
So, write a
as a higher order function, AKA HOF: specifically, a function that takes a function object as an argument, and returns a function object.
As you give NO idea in your question about what a
is supposed to DO, you're really making it impossible to give a code example that makes any sense whatsoever: good English or not, you're really polluting SO, not contributing to it, by your questions, since you never explain WHAT are you trying to accomplish in your code!!!
def a(b):
print 'xxx'
return b
@a
def b():
return 'bbb'
b()
This is the same as:
def a(b):
print 'xxx'
return b
def b():
return 'bbb'
b = a(b)
b()
精彩评论