i can't understand decorator

Discussion in 'Python' started by contro opinion, Jan 15, 2013.

  1. >>> 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.
    contro opinion, Jan 15, 2013
    #1
    1. Advertising

  2. 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
    Thomas Rachel, Jan 15, 2013
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sakesun Roykiattisak
    Replies:
    1
    Views:
    240
    Christopher T King
    Aug 5, 2004
  2. glomde
    Replies:
    5
    Views:
    518
    glomde
    Mar 29, 2007
  3. Saqib Ali
    Replies:
    4
    Views:
    198
    Ethan Furman
    Dec 22, 2011
  4. Oscar Benjamin

    Re: i can't understand decorator

    Oscar Benjamin, Jan 15, 2013, in forum: Python
    Replies:
    0
    Views:
    122
    Oscar Benjamin
    Jan 15, 2013
  5. Mark Carter
    Replies:
    2
    Views:
    159
    Steven D'Aprano
    Jan 18, 2013
Loading...

Share This Page