Re: what does 'a=b=c=[]' do



On Dec 21, 5:44 pm, Steven D'Aprano <steve
+comp.lang.pyt...@xxxxxxxxxxxxx> wrote:

Yes, you should create your lists before trying to append to them.

But you aren't forced to use a for-loop. You can use a list comprehension:

x = [some_function(a) for a in range(n)]

Notice that here you don't need x to pre-exist, because the list comp
creates a brand new list, which then gets assigned directly to x.

Now to my actual question.  I need to do the above for multiple arrays
(all the same, arbitrary size).  So I do this:
   x=y=z=[]

This creates one empty list object, and gives it three names, x, y and z.
Every time you append to the list, all three names see the same change,
because they refer to a single list.

[...]

Except it seems that I didn't create three different arrays, I created
one array that goes by three different names (i.e. x[], y[] and z[] all
reference the same pile of numbers, no idea which pile).

Exactly.

This surprises me, can someone tell me why it shouldn't?

Because that's the way Python works. Python is an object-oriented, name
binding language. This is how OO name binding works: you have a single
object, with three names bound to it. The above line is short-cut for:

a = []
b = a
c = a

Python does not make a copy of the list unless you specifically instruct
it to.

I figure if I
want to create and initialize three scalars the just do "a=b=c=7",

That creates a single integer object with value 7, and binds three names
to it, *exactly* the same as the above.

If you could modify int objects in place, like you can modify lists in
place, you would see precisely the same effect. But ints are immutable:
all operations on ints create new ints. Lists are mutable, and can be
changed in place.

for
example, so why not extend it to arrays.  Also, is there a more pythonic
way to do "x=[], y=[], z=[]"?

Well that literally won't work, you can't separate them by commas.
Newlines or semicolons will work.

Or: x, y, z = [], [], []

Either is pretty Pythonic.

--
Steven

Thanks to you and Dennis for the quick lesson and tips. Very helpful
and illuminating.
.



Relevant Pages

  • Re: why cannot assign to function call
    ... I'm going to follow up here at the risk of annoying Mark, ... helpful in explaining things to Python beginners. ... it becomes a namespace mapping names to objects. ... to the list itself, while Steven held that Python lists ...
    (comp.lang.python)
  • python-dev Summary for 2003-10-16 through 2003-11-15
    ... This is the twenty-eighth and twenty-ninth summaries written by Brett ... The in-development version of the documentation for Python can be found ... If you have ever wanted to see linked lists used in Python in a rather ... Contributing threads: ...
    (comp.lang.python)
  • TOC of Python Cookbook now online (was Re: author index for Python Cookbook 2?)
    ... Processing a String One Character at a Time ... Finding a File on the Python Search Path ... Constructing Lists with List Comprehensions ... Looping over Items and Their Indices in a Sequence ...
    (comp.lang.python)
  • chapter4
    ... The for statement in Python differs a bit from what you may be used to ... list or a string), in the order that they appear in the sequence. ... (this can only happen for mutable sequence types, such as lists). ... The keyword def introduces a function definition. ...
    (Ubuntu)
  • Re: More pythonic shell sort?
    ... So I decided to tackle this old school problem with the python mindset. ... for gap in self.gapSeq: ... I didn't really think of pop and insert as advanced features. ... that they are python lists, ...
    (comp.lang.python)