Strange behavior when printing a returned closure function

Discussion in 'Python' started by dartsch@dicad.de, Mar 25, 2007.

  1. Guest

    Hello,

    when I execute the following code (python 2.5)

    def f(x):
    def g():
    return x
    return g

    print f(1)
    print f(2)

    I get an output like

    <function g at 0x00AFC1F0>
    <function g at 0x00AFC1F0>

    So according to print I get the same function object returned at both
    calls.
    That's surprising, I would expect to get two distinct function objects
    because their func_closure attribute has to be different. And indeed,
    if I do

    print f(1) is f(2)

    instead, it prints False. Even more confusing, if I do

    g1 = f(1)
    g2 = f(2)
    print g1
    print g2

    I get something like

    <function g at 0x00AFC1B0>
    <function g at 0x00AFC1F0>

    ie. two distinct function objects are printed.

    What's happening here?
    Some clever optimization reusing function objects in special cases or
    what ...?

    Thomas
     
    , Mar 25, 2007
    #1
    1. Advertising

  2. Guest

    On Mar 25, 1:04 pm, Jean-Paul Calderone <> wrote:
    > On 25 Mar 2007 03:59:52 -0700, wrote:
    >
    >
    >
    > >Hello,

    >
    > >when I execute the following code (python 2.5)

    >
    > >def f(x):
    > > def g():
    > > return x
    > > return g

    >
    > >print f(1)
    > >print f(2)

    >
    > >I get an output like

    >
    > ><function g at 0x00AFC1F0>
    > ><function g at 0x00AFC1F0>

    >
    > >So according to print I get the same function object returned at both
    > >calls.
    > >That's surprising, I would expect to get two distinct function objects
    > >because their func_closure attribute has to be different. And indeed,
    > >if I do

    >
    > >print f(1) is f(2)

    >
    > >instead, it prints False. Even more confusing, if I do

    >
    > >g1 = f(1)
    > >g2 = f(2)
    > >print g1
    > >print g2

    >
    > >I get something like

    >
    > ><function g at 0x00AFC1B0>
    > ><function g at 0x00AFC1F0>

    >
    > >ie. two distinct function objects are printed.

    >
    > >What's happening here?
    > >Some clever optimization reusing function objects in special cases or
    > >what ...?

    >
    > They're _not_ the same function object, just like the `is' test told you.
    > They just happen to have been allocated at the same memory address.
    >
    > Jean-Paul


    ah yes, I see, thanks
     
    , Mar 25, 2007
    #2
    1. Advertising

  3. On Sun, 25 Mar 2007 03:59:52 -0700, dartsch wrote:


    > I get an output like
    >
    > <function g at 0x00AFC1F0>
    > <function g at 0x00AFC1F0>
    >
    > So according to print I get the same function object returned at both
    > calls.


    Not the same function object. The first object is printed, then deleted
    by the garbage collector because it goes out of scope. Then the second one
    is created and just happens to end up in the same memory location. That's
    an accident of the garbage collector implementation.


    > That's surprising, I would expect to get two distinct function objects
    > because their func_closure attribute has to be different. And indeed,
    > if I do


    [snip]

    > <function g at 0x00AFC1B0>
    > <function g at 0x00AFC1F0>
    >
    > ie. two distinct function objects are printed.


    This time the first function still exists when the second is created, so
    the second naturally can't be in the same memory location.



    --
    Steven.
     
    Steven D'Aprano, Mar 25, 2007
    #3
    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. Victor Ng
    Replies:
    1
    Views:
    338
    Bengt Richter
    Mar 11, 2005
  2. Replies:
    9
    Views:
    275
    Dennis Lee Bieber
    Apr 17, 2007
  3. SeanMon

    Function closure inconsistency

    SeanMon, Jul 23, 2010, in forum: Python
    Replies:
    3
    Views:
    248
    Terry Reedy
    Jul 23, 2010
  4. Julian Mehnle
    Replies:
    0
    Views:
    239
    Julian Mehnle
    Jul 17, 2003
  5. LAN MIND

    What is function closure?

    LAN MIND, Jan 27, 2008, in forum: Javascript
    Replies:
    5
    Views:
    83
    Thomas 'PointedEars' Lahn
    Jan 27, 2008
Loading...

Share This Page