Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: i can't understand decorator

Reply
Thread Tools

Re: i can't understand decorator

 
 
Oscar Benjamin
Guest
Posts: n/a
 
      01-15-2013
On 15 January 2013 14:20, contro opinion <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
i can't understand decorator contro opinion Python 1 01-15-2013 09:21 PM
Why doesnt __getattr__ with decorator dont call __get_method in decorator glomde Python 5 03-29-2007 02:48 PM
layout decorator framework that fits well w/ spring William Z. Java 0 03-22-2006 11:19 PM
Read all of this to understand how it works. then check around on otherRead all of this to understand how it works. then check around on other thelisa martin Computer Support 2 08-18-2005 06:40 AM
explanations about the Decorator design pattern Jean Lutrin Java 8 11-18-2004 05:40 PM



Advertisments