i can't understand decorator

contro opinion contropinion at gmail.com
Tue Jan 15 09:20:04 EST 2013


>>> def deco(func):
...      def kdeco():
...          print("before myfunc() called.")
...          func()
...          print("  after myfunc() called.")
...      return kdeco
...
>>> @deco
... def myfunc():
...      print(" myfunc() called.")
...
>>> myfunc()
before myfunc() called.
 myfunc() called.
  after myfunc() called.
>>> deco(myfunc)()
before myfunc() called.
before myfunc() called.
 myfunc() called.
  after myfunc() called.
  after myfunc() called.
1.
why there are two lines :before myfunc() called.and tow lines :after
myfunc() called. in the output?
2.why the result is not
before myfunc() called.
 myfunc() called.
  after myfunc() called.
before myfunc() called.
 myfunc() called.
  after myfunc() called.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130115/5a4d350d/attachment.html>


More information about the Python-list mailing list