471,350 Members | 1,665 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Globals in nested functions

def f():
a = 12
def g():
global a
if a < 14:
a=13
g()
return a

print f()

This function raises an error. Is there any way to access the a in f()
from inside g().

I could find few past discussions on this subject, I could not find
the simple answer whether it is possible to do this reference.

-
Suresh

Jun 21 '07 #1
4 1198
"jm*******@no.spam.gmail.com" <jm*******@gmail.comwrote:
def f():
a = 12
def g():
global a
if a < 14:
a=13
g()
return a

print f()

This function raises an error. Is there any way to access the a in f()
from inside g().

I could find few past discussions on this subject, I could not find
the simple answer whether it is possible to do this reference.
'global' means global to the module, it prevents the lookup happening in
current or nested scopes.

Simple answer:

You can access an object referenced by a nested scope variable and you
can mutate the object accessed in that way, but you cannot rebind the
name to a different object without resorting to hackery.

To get the effect you want, simply use a mutable object:
>>def f():
class v:
a = 12
def g():
if v.a < 14:
v.a=13
g()
return v.a
>>f()
13

and as soon as the code starts looking at all complex, refactor that so
the class is the thing you interact with:
>>class F(object):
def g(self):
if self.a < 14:
self.a = 13
def __call__(self):
self.a = 12
self.g()
return self.a
>>f = F()
f()
13
Jun 21 '07 #2
jm*******@no.spam.gmail.com wrote:
def f():
a = 12
def g():
global a
if a < 14:
a=13
g()
return a

print f()

This function raises an error. Is there any way to access the a in
f() from inside g().
Yes. Pass it to g when calling the latter and let g return the
result.

def f():
a = 12

def g(parm):
if parm < 14:
return 13
else:
return a

a = g(a)
return a

print f()

Strange refactoring though.

Regards,
Björn

--
BOFH excuse #400:

We are Microsoft. What you are experiencing is not a problem; it is
an undocumented feature.

Jun 21 '07 #3
jm*******@no.spam.gmail.com wrote:
def f():
a = 12
def g():
global a
if a < 14:
a=13
g()
return a

print f()

This function raises an error. Is there any way to access the a in f()
from inside g().

I could find few past discussions on this subject, I could not find
the simple answer whether it is possible to do this reference.

-
Suresh
As I'm struggling with this myself at the moment,
this will do the job:

def f():
global a
a = 12

cheers,
Stef Mientki
Jun 21 '07 #4
On 2007-06-21, jm*******@no.spam.gmail.com <jm*******@gmail.comwrote:
def f():
a = 12
def g():
global a
if a < 14:
a=13
g()
return a

print f()

This function raises an error. Is there any way to access the a
in f() from inside g().

I could find few past discussions on this subject, I could not
find the simple answer whether it is possible to do this
reference.
Python's scoping rules don't allow this. But you can 'box' the
value into a list and get the intended effect.

def f():
a = [12]
def g():
if a[0] < 14:
a[0] = 13
g()
return a[0]

You'll get better results, in Python, by using a class instances
instead of closures. Not that there's anything wrong with Python
closures, but the scoping rules make some fun tricks too tricky.

--
Neil Cerutti
Jun 21 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by lawrence | last post: by
3 posts views Thread by Robert Dodier | last post: by
6 posts views Thread by Paddy | last post: by
11 posts views Thread by Matt | last post: by
18 posts views Thread by robert | last post: by
17 posts views Thread by meridian | last post: by
5 posts views Thread by Steven W. Orr | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.