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


Groups > comp.lang.python > #36853 > unrolled thread

i can't understand decorator

Started bycontro opinion <contropinion@gmail.com>
First post2013-01-15 22:20 +0800
Last post2013-01-15 22:21 +0100
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  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

#36853 — i can't understand decorator

Fromcontro opinion <contropinion@gmail.com>
Date2013-01-15 22:20 +0800
Subjecti can't understand decorator
Message-ID<mailman.537.1358259607.2939.python-list@python.org>

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

[toc] | [next] | [standalone]


#36869

FromThomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
Date2013-01-15 22:21 +0100
Message-ID<kd4h8l$6rs$1@r03.glglgl.gl>
In reply to#36853
Am 15.01.2013 15:20 schrieb contro opinion:

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

Wrapping works this way:

The function is defined, and the wrapper replaces the function with a 
different one which (in this case) calls the original one.

Try print(myfunc) here and you see that myfunc is only a name for 
another function called kdeco. It is the one returned by the decorator.


> 1.
> why there are two lines :before myfunc() called.and tow lines :after
> myfunc() called. in the output?

This is because the "before" line is printed, then the modified "myfunc" 
is called, which in turn prints another "before" line and then calls the 
"really original" function. After it returns, the "after" line is called 
by the inner placement function (the one which sticks at the myfunc 
identifier). This function returns and the function instance which 
called the first "before" line is printed then.

> 2.why the result is not
> before myfunc() called.
>   myfunc() called.
>    after myfunc() called.
> before myfunc() called.
>   myfunc() called.
>    after myfunc() called.

Because the function calls are wrapped and not repeated.


Thomas

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web