Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

i can't understand decorator

 
 
contro opinion
Guest
Posts: n/a
 
      01-15-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.

 
Reply With Quote
 
 
 
 
Thomas Rachel
Guest
Posts: n/a
 
      01-15-2013
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
 
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
Re: i can't understand decorator Oscar Benjamin Python 0 01-15-2013 02:31 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