function in a function accessing vars

J

Jorgen Bodde

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
 
D

Diez B. Roggisch

Jorgen said:
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.
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
 
J

Jorgen Bodde

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

Jorgen said:
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.
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
 
D

Dustan

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top