Functions as Objects, and persisting values

Discussion in 'Python' started by Falcolas, Nov 5, 2007.

  1. Falcolas

    Falcolas Guest

    Please help me understand the mechanics of the following behavior.

    >>> def d():

    header = 'I am in front of '
    def e(something):
    print header + something
    return e

    >>> f = d()
    >>> f('this')

    I am in front of this
    >>> del(d)
    >>> f('this')

    I am in front of this

    The way I understand it, function d is an object, as is e. However I
    don't quite grok the exact relationship between e and d. Is e
    considered to be a subclass of 'd', so that it has access to it's
    parent's __dict__ object, in order to access the value of 'header'? Or
    is this persistence managed in a different fashion?
     
    Falcolas, Nov 5, 2007
    #1
    1. Advertising

  2. Falcolas schrieb:
    > Please help me understand the mechanics of the following behavior.
    >
    >>>> def d():

    > header = 'I am in front of '
    > def e(something):
    > print header + something
    > return e
    >
    >>>> f = d()
    >>>> f('this')

    > I am in front of this
    >>>> del(d)
    >>>> f('this')

    > I am in front of this
    >
    > The way I understand it, function d is an object, as is e. However I
    > don't quite grok the exact relationship between e and d. Is e
    > considered to be a subclass of 'd', so that it has access to it's
    > parent's __dict__ object, in order to access the value of 'header'? Or
    > is this persistence managed in a different fashion?


    The "thing" you observe here is a called a closure. It consists of the
    local variables surrounding e. So as long as you keep a reference to e,
    you keep one to the variables of d itself.

    Diez
     
    Diez B. Roggisch, Nov 5, 2007
    #2
    1. Advertising

  3. Falcolas

    Rich Harkins Guest

    [snip]
    > The "thing" you observe here is a called a closure. It consists of the
    > local variables surrounding e. So as long as you keep a reference to e,
    > you keep one to the variables of d itself.
    >
    > Diez


    More specifically though it keeps references to the requested variables
    only:


    def closed():
    x = global_x
    y = "Y_VAR"
    def inner():
    return y
    return inner

    class Destroyable(object):
    def __del__(self):
    print "DESTROYED"

    global_x = Destroyable()
    inner = closed()
    print inner()
    del global_x
    print inner()
    print "HERE"

    You will get:

    Y_VAR
    DESTROYED
    Y_VAR
    HERE

    If the entire dict of closed() was kept you would have seen:

    Y_VAR
    Y_VAR
    HERE
    DESTROYED

    Since closed hadn't been destroyed yet: thus there was only one
    reference remaining to global_x after closed() and inner() were called.

    Rich
     
    Rich Harkins, Nov 5, 2007
    #3
  4. Falcolas a écrit :
    > Please help me understand the mechanics of the following behavior.
    >
    >
    >>>>def d():

    >
    > header = 'I am in front of '
    > def e(something):
    > print header + something
    > return e
    >
    >
    >>>>f = d()
    >>>>f('this')

    >
    > I am in front of this
    >
    >>>>del(d)
    >>>>f('this')

    >
    > I am in front of this
    >
    > The way I understand it, function d is an object,


    Right.

    > as is e.


    Right.

    > However I
    > don't quite grok the exact relationship between e and d.
    >
    > Is e
    > considered to be a subclass of 'd',


    Nope.

    > so that it has access to it's
    > parent's __dict__ object, in order to access the value of 'header'? Or
    > is this persistence managed in a different fashion?


    As Diez said, it's called a lexical closure. A lexical closure is a
    function that captures and carries the lexical scope it was defined in
    with it - it 'closes over' it's environnement. Each time you call d, it
    returns a new function object (using the same code object) with a
    different environnement. You'll find this environement in f.func_closure.
     
    Bruno Desthuilliers, Nov 5, 2007
    #4
    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. headware

    Persisting business layer objects

    headware, Jun 16, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    458
    headware
    Jun 16, 2004
  2. Carlos
    Replies:
    3
    Views:
    5,057
    PeterKellner
    May 17, 2006
  3. Chris S.

    Persisting Dynamic Objects?

    Chris S., Jun 26, 2004, in forum: Python
    Replies:
    2
    Views:
    303
    Chris S.
    Jun 27, 2004
  4. highlander

    persisting dynamic objects in java

    highlander, May 21, 2007, in forum: Java
    Replies:
    3
    Views:
    683
    Richard Reynolds
    Jun 2, 2007
  5. VisionSet
    Replies:
    5
    Views:
    143
    Randy Webb
    Jul 21, 2006
Loading...

Share This Page