Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #36855 > unrolled thread
| Started by | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
|---|---|
| First post | 2013-01-15 14:31 +0000 |
| Last post | 2013-01-15 14:31 +0000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: i can't understand decorator Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-01-15 14:31 +0000
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
|---|---|
| Date | 2013-01-15 14:31 +0000 |
| Subject | Re: i can't understand decorator |
| Message-ID | <mailman.539.1358260288.2939.python-list@python.org> |
On 15 January 2013 14:20, contro opinion <contropinion@gmail.com> wrote:
>>>> 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?
You have wrapped the function twice with the decorator. Try changing the line
print("before func() called")
to
print("about to call", func,__name__)
and you'll see that the function it is about to call is not the same
in both cases.
> 2.why the result is not
> before myfunc() called.
> myfunc() called.
> after myfunc() called.
> before myfunc() called.
> myfunc() called.
> after myfunc() called.
You would get this output if you just called myfunc() twice. I don't
know why you expect wrapping the function twice to have this effect.
Oscar
Back to top | Article view | comp.lang.python
csiph-web