views:

87

answers:

2

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 English is not very good. Thank you.

+1  A: 
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()
Knio
+3  A: 

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!!!

Alex Martelli
+1 even though I hate using ALL CAPS to emphasize things (that's what bold and italics are for).
Chris Lutz
thank you ,alex
zjm1126