multivariable assignment

D

davidj411

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']
 
A

Andreas Waldenburger

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.
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
 
J

John Posner

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'".

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
 
L

Lie Ryan

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']

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
 
R

Rainer Grimm

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']
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top