Scope
Elliot Temple
curi at curi.us
Fri Jun 3 22:03:40 EDT 2005
I want to write a function, foo, so the following works:
def main():
n = 4
foo(n)
print n
#it prints 7
if foo needs to take different arguments, that'd be alright.
Is this possible?
I already tried this (below), which doesn't work. foo only changes
the global n.
n = 3
def main():
def foo(var, context, c2):
exec var + " = 7" in context, c2
n = 4
foo("n", locals(), globals())
print n
if __name__ == '__main__': main()
print n
And of course I tried:
>>> def inc(n):
... n += 3
...
>>> a = 4
>>> inc(a)
>>> a
4
-- Elliot Temple
http://www.curi.us/
---
[This E-mail scanned for viruses by Declude Virus]
More information about the Python-list
mailing list