Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

multivariable assignment

0 views
Skip to first unread message

davidj411

unread,
Dec 31, 2009, 11:13:35 AM12/31/09
to
I am not sure why this behavior is this way.
at beginning of script, i want to create a bunch of empty lists and
use each one for its own purpose.
however, updating one list seems to update the others.

>>> a = b = c = []
>>> a.append('1')
>>> a.append('1')
>>> a.append('1')
>>> c
['1', '1', '1']
>>> a
['1', '1', '1']
>>> b
['1', '1', '1']

J

unread,
Dec 31, 2009, 11:27:51 AM12/31/09
to davidj411, pytho...@python.org

That's because you're saying c is an empty list object, b is a pointer
to the same list object and a is a pointer to the same list object.
It looks better if you look at it like this:

a \
b ------ []
c /

You just created one actual object and created three pointers to it.

Though I wondered something similar the other day...

obviously this won't work:

a,b,c = []

but does this do it properly:

a,b,c = [],[],[]

For now, in my newbieness, I've been just creating each one separately:

a=[]
b=[]
c=[]

Cheers,

Jeff
--

Jonathan Swift - "May you live every day of your life." -
http://www.brainyquote.com/quotes/authors/j/jonathan_swift.html

Andreas Waldenburger

unread,
Dec 31, 2009, 11:32:04 AM12/31/09
to
On Thu, 31 Dec 2009 08:13:35 -0800 (PST) davidj411
<davi...@gmail.com> wrote:

> I am not sure why this behavior is this way.
> at beginning of script, i want to create a bunch of empty lists and
> use each one for its own purpose.
> however, updating one list seems to update the others.
>
> >>> a = b = c = []

No, you're only creating one list (and giving that one list three
names). Creating three lists would be:

a = []
b = []
c = []

or, alternatively

a, b, c = [], [], []

Just remember: Python "variables" are just names that point to the
actual data. "x = y" just means "make 'x' a synonym for 'y'".

/W


--
INVALID? DE!

John Posner

unread,
Dec 31, 2009, 2:19:32 PM12/31/09
to

Revising/expanding Andreas's last paragraph (trying to be correct, but not
trying to be complete):

Python deals with NAMEs and OBJECTs. A NAME is "bound" or "assigned" to an
OBJECT. Each OBJECT can have any number of NAMEs. A NAME cannot be
assigned to another NAME -- only to an OBJECT.

Objects are created by EXPRESSIONS, such as:

42
42 + 3*othernum
"spam"
"spam" + "eggs"
empname[4:10] <-- slice of a string or other sequence
cook("spam" + "eggs", 7) <-- calling a function
"spam".split() <-- calling an object method
Brunch("spam", veggie, dessert) <-- instantiating a class

NAMEs are created by assignment statements. You can also reuse an existing
NAME in a subsequent assignment statement.

Some assignment statements are of the form:

NAME = EXPRESSION

Ex:

grand_total = 42
paragraph = " ".join(sentence1, sentence2)
grand_total = 42 + 3*othernum
sunday_brunch = Brunch(Eggs(2), "broccoli", "cookie")

This form:

1a. Creates a new NAME,
or
1b. Removes an existing NAME from the OBJECT to which it is currently
assigned.
2. Assigns the NAME to the OBJECT created by the EXPRESSION.

Other assignment statements are of the form:

NAME2 = NAME1

This form does not create a new OBJECT. Instead, it assigns NAME2 as an
additional name for the object to which NAME1 is currently assigned. Ex:

y = x
tenth_item = mylist[9] <-- a list's items have auto-assigned integer
NAMEs
clr = colors["sea foam"] <-- a dict has user-devised NAMEs called "keys"
red_comp = rgb_color.red <-- a class instance has user-devised NAMEs
called "attributes"

My favorite metaphor for Python's NAMEs-and-OBJECTs scheme is the
Post-It(r). NAMEs are like Post-Its; you can stick any number of Post-Its
to any object.

HTH (and sorry if the above is overly pedantic),
John

Lie Ryan

unread,
Jan 1, 2010, 8:03:32 AM1/1/10
to

Every time people get confused because of how python object model works,
I always refer them to this article:
http://effbot.org/zone/call-by-object.htm

Rainer Grimm

unread,
Jan 2, 2010, 2:18:38 AM1/2/10
to
a, b and c are the same objects.
You can check the object identity by

>>> a = b = c = []
>>> print id(a),id(b),id(c)
3083044140 3083044140 3083044140
>>> a is b is c
True

Greetings

0 new messages