Chris Rebert於 2012年1月10日星期二UTC+8下午1時15分53秒寫道: > On Mon, Jan 9, 2012 at 8:14 PM, contro opinion <contro...@gmail.com> wrote: > > test1.py > > > > def deco(func): > > print 'i am in deco' > > > > @deco > > def test(): > > print 'i am in test' > > > > > > when you run it ,you get : > > i am in deco > > > > > > > > test2.py > > > > def tsfunc(func): > > def wrappedFunc(): > > print 'i am in deco' > > return func() > > return wrappedFunc > > > > @tsfunc > > def test(): > > print "i am in test" > > > > when you run test2.py,you can get nothing ,why can't i get :?? > > i am in deco > Try to add testgen=test() or testgent=test in test2.py
testgen() # a function is an object, Also you can wrap a function to use yield instead of return. It is trivial to get a call back function in python by a decorator that wraps a non-call back function. > Because that `print` is in wrappedFunc() [which in this case is A.K.A. > test()], which is only defined, never called. Try adding a call to > test() at the end of your script. > Contrast this with deco(), which has the `print` directly in its body > rather than inside a nested function definition. > > Also, might I suggest reading a tutorial on decorators or asking on an > IRC channel, rather than pelting the mailing list with all these > simple and related questions in such a fairly short timeframe? > At least please keep all these in a single thread instead of starting > a new one every single time... > > Cheers, > Chris -- http://mail.python.org/mailman/listinfo/python-list