Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #36853

i can't understand decorator

Date 2013-01-15 22:20 +0800
Subject i can't understand decorator
From contro opinion <contropinion@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.537.1358259607.2939.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

i can't understand decorator contro opinion <contropinion@gmail.com> - 2013-01-15 22:20 +0800
  Re: i can't understand decorator Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2013-01-15 22:21 +0100

csiph-web