Namespace Mystery (HELP!)

Warren L. DeLano warren at sunesis-pharma.com
Thu Jul 6 23:49:37 EDT 2000


In the following code (with Python 1.5.2)...

**************
a = 1
def fn():
   b = 1
   globals()['a']=2
   locals()['b']=2
   print a,b

fn()
*************

Why is the output "2 1" and not "2 2"?
What is it about the locals() function that makes it behave
unexpectedly?

Note that the same unexpected result also occurs with exec.

def fn():
   b=1
   exec "b=2" in globals(),locals()
   print b   # output is "1"

does not produce the same result as

def fn():
   b=1
   exec "b=2"
   print b  # output is "2"

The former example does not modify the function's local name space,
where the latter does...my expectation would be that both examples
should modify the local namespace.  Has this behavior changed in 1.6?

Insight from a Python Guru would be much appreciated!

Thanks,
- Warren

--
mailto:warren at sunesis-pharma.com
Warren L. DeLano, Ph.D.
Informatics Scientist
Sunesis Pharmaceuticals, Inc.
3696 Haven Ave., Suite C
Redwood City, CA 94063
(650)-556-8800 fax: (650)-556-8824





More information about the Python-list mailing list