function in a function accessing vars

Discussion in 'Python' started by Jorgen Bodde, Jun 6, 2007.

  1. Jorgen Bodde

    Jorgen Bodde Guest

    Hi all,

    I wanted to solve a small problem, and I have a function that is
    typically meant only as a function belonging inside another function.
    >From the inner function I want to access a variable from the outer

    function like;

    def A():
    some_var = 1
    def B():
    some_var += 1

    B()


    But this does not work, the function B does not recognize the
    some_var. In my mind I thought the scope would propagate to the new
    function and the vars would still be accessible.

    How can I go about this?

    With regards
    - Jorgen
     
    Jorgen Bodde, Jun 6, 2007
    #1
    1. Advertisements

  2. Jorgen Bodde wrote:

    > Hi all,
    >
    > I wanted to solve a small problem, and I have a function that is
    > typically meant only as a function belonging inside another function.
    >>From the inner function I want to access a variable from the outer

    > function like;
    >
    > def A():
    > some_var = 1
    > def B():
    > some_var += 1
    >
    > B()
    >
    >
    > But this does not work, the function B does not recognize the
    > some_var. In my mind I thought the scope would propagate to the new
    > function and the vars would still be accessible.
    >
    > How can I go about this?


    The problem here is the way python determines which variables are local to a
    function - by inspecting left sides.

    I'm not sure if there are any fancy inspection/stackframe/cells-hacks to
    accomplish what you want. But the easiest solution seems to be a
    (admittedly not too beautiful)

    def A():
    some_var = [1]
    def B(v):
    v[0] += 1

    B(some_var)


    Or you should consider making A a callable class and thus an instance, and
    some_var an instance variable. Always remember: "a closure is a poor
    persons object, and an object is a poor mans closure"

    Diez
     
    Diez B. Roggisch, Jun 6, 2007
    #2
    1. Advertisements

  3. Jorgen Bodde

    Jorgen Bodde Guest

    Hi Diez,

    Thanks, I thought it worked similar to C++ where a higher compound
    could access a lower section. But as it is not straight forward, I
    think it is better to embed the functionality inside a class, and make
    it a member variable .. now why didn't I think of that ;-)

    Thanks,
    - Jorgen

    On 6/6/07, Diez B. Roggisch <> wrote:
    > Jorgen Bodde wrote:
    >
    > > Hi all,
    > >
    > > I wanted to solve a small problem, and I have a function that is
    > > typically meant only as a function belonging inside another function.
    > >>From the inner function I want to access a variable from the outer

    > > function like;
    > >
    > > def A():
    > > some_var = 1
    > > def B():
    > > some_var += 1
    > >
    > > B()
    > >
    > >
    > > But this does not work, the function B does not recognize the
    > > some_var. In my mind I thought the scope would propagate to the new
    > > function and the vars would still be accessible.
    > >
    > > How can I go about this?

    >
    > The problem here is the way python determines which variables are local to a
    > function - by inspecting left sides.
    >
    > I'm not sure if there are any fancy inspection/stackframe/cells-hacks to
    > accomplish what you want. But the easiest solution seems to be a
    > (admittedly not too beautiful)
    >
    > def A():
    > some_var = [1]
    > def B(v):
    > v[0] += 1
    >
    > B(some_var)
    >
    >
    > Or you should consider making A a callable class and thus an instance, and
    > some_var an instance variable. Always remember: "a closure is a poor
    > persons object, and an object is a poor mans closure"
    >
    > Diez
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Jorgen Bodde, Jun 6, 2007
    #3
  4. Jorgen Bodde

    Dustan Guest

    On Jun 6, 6:40 am, "Jorgen Bodde" <> wrote:
    > Hi Diez,
    >
    > Thanks, I thought it worked similar to C++ where a higher compound
    > could access a lower section.


    It can 'access a lower section'; what it can't do is *change* that
    'lower section'; in your example case with an int, this matters
    because ints are immutable. Lists, on the other hand, are mutable. You
    can *access* the methods of the list that mutate it. You're always
    working with the same list, but it has different contents when you
    mutate it.

    > But as it is not straight forward, I
    > think it is better to embed the functionality inside a class, and make
    > it a member variable .. now why didn't I think of that ;-)
     
    Dustan, Jun 6, 2007
    #4
    1. Advertisements

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. Jon

    app vars and cache vars

    Jon, Dec 14, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    450
  2. Fernando Rodríguez

    Trouble accessing global vars

    Fernando Rodríguez, Sep 4, 2004, in forum: Python
    Replies:
    6
    Views:
    476
    Alex Martelli
    Sep 6, 2004
  3. Martin Drautzburg

    accessing module global vars by name

    Martin Drautzburg, Dec 20, 2004, in forum: Python
    Replies:
    2
    Views:
    295
    Peter Hansen
    Dec 20, 2004
  4. Linuxguy123
    Replies:
    7
    Views:
    759
    Paddy O'Loughlin
    Feb 20, 2009
  5. caccolangrifata
    Replies:
    18
    Views:
    444
    Chris Torek
    Jul 22, 2011
Loading...

Share This Page