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

Joshua Landau joshua.landau.ws at gmail.com
Wed Dec 21 18:06:11 EST 2011


On 21 December 2011 22:25, Eric <einazaki668 at yahoo.com> wrote:

> Is it true that if I want to create an array or arbitrary size such
> as:
>   for a in range(n):
>      x.append(<some function...>)
>
> I must do this instead?
>   x=[]
>   for a in range(n):
>      x.append(<some function...>)
>
> 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=[]
>   for a in range(n):
>      x.append(<some function...>)
>      y.append(<some other function...>)
>      z.append(<yet another function...>)
>
> 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).
>
> This surprises me, can someone tell me why it shouldn't?  I figure if
> I want to create and initialize three scalars the just do "a=b=c=7",
>

7 is 7 => True
They're the same "7". You won't notice it though, as numbers are immutable.

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

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

It's a slick language but I still have trouble wrapping my brain
> around some of the concepts.
>
> TIA,
> eric
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20111221/caadfdbf/attachment-0001.html>


More information about the Python-list mailing list