Namespace Mystery (HELP!)
Dag Sunde
dag at orion.no
Fri Jul 7 04:44:06 EDT 2000
Warren,
Aside from your question about the locals() and Pauls
excellent answer, is there any particular reason
you're not writing something like:
>>> def fnx():
... b = 1
... global a
... a = 2
... b = 2
... print a, b
Dag.
"Paul Prescod" <paul at prescod.net> wrote in message
news:39656345.86AADA8A at prescod.net...
> > Why is the output "2 1" and not "2 2"?
> > What is it about the locals() function that makes it behave
> > unexpectedly?
>
> locals ()
> Return a dictionary representing the current local symbol table.
> Warning: the contents of this dictionary should not be modified;
changes
> may not affect the values of local variables used by the interpreter.
>
> http://www.python.org/doc/lib/built-in-funcs.html
>
> Python doesn't typically look up local variables based on a string
name
> at runtime. That would be too inefficient. Rather it looks them up
> positionally.
>
> --
> Paul Prescod - Not encumbered by corporate consensus
> Pop stars come and pop stars go, but amid all this change there is one
> eternal truth: Whenever Bob Dylan writes a song about a guy, the guy
is
> guilty as sin.
> - http://www.nj.com/page1/ledger/e2efc7.html
>
More information about the Python-list
mailing list