Namespace issues...

vincent wehren vincent at visualtrans.de
Sat Feb 14 06:13:49 EST 2004


"cghost" <phreacker at aol.com> schrieb im Newsbeitrag
news:d1253dfc.0402140259.3b614b95 at posting.google.com...
| i am very confused...why does the following script modify the global list
"l":
|
| l=[]
|
| def x():
|    l.append("xyz")
|
| x()
| print l
|
| but the same script applied to a single variable doesnt..:
|
| l="moe"
|
| def x():
|    l+="howdy"

Python decides that a variable is local if it's ever *assigned a value in a
function*.
In  this example you assign a value to l. In the first example, you do not
(you change list l
"in place"), so here this optimization does not take place.

You have to be explicit about l being a global if you assign a value to it
using the "global" statement as in:
def x():
    global l
    l+="howdy"

HTH,

Vincent Wehren



|
| x()
| print l





More information about the Python-list mailing list