By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,171 Members | 809 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,171 IT Pros & Developers. It's quick & easy.

Namespace issues...

P: n/a
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"

x()
print l
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"cghost" <ph*******@aol.com> schrieb im Newsbeitrag
news:d1**************************@posting.google.c om...
| 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
Jul 18 '05 #2

P: n/a
cghost wrote:
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"

x()
print l


It's even worse:
def add(a, b): .... a += b
....

Now consider
lst = []
add(lst, ["alpha"])
lst ['alpha'] add(lst, ["beta", "gamma"])
lst ['alpha', 'beta', 'gamma']

where add(a, b) appends b to a versus
tpl = ()
add( tpl, ("alpha",))
tpl ()

where no apparent change takes place. The difference is that list is a
mutable type i. e. its instances can be changed anytime, whereas tuples are
immutable, i. e. they cannot be changed once they are created. For lists a
+= b is implemented to append the items in b to a. The binding of a is not
changed in the process. For immutable types this is not an option - they
cannot be altered. Therefore, for tuples a += b is implemented as a = a +
b, i. e. a new tuple containing the elements of both a and b is created and
the variable a is bound to the new tuple. In the example the binding takes
place inside a function, so you never see the new tuple unless you return
it or declare the variable as global as Vincent already pointed out.
Strings are immutable too, so
s = "abc"
add(s, "def")
s

'abc'

as expected (hopefully).

Peter
Jul 18 '05 #3

P: n/a
In article <d1**************************@posting.google.com >,
cghost <ph*******@aol.com> wrote:

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"
x()
print l


http://starship.python.net/crew/mwh/...jectthink.html
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"Argue for your limitations, and sure enough they're yours." --Richard Bach
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.