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



On Dec 22, 8:25 am, Eric <einazaki...@xxxxxxxxx> wrote:
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",
for example, so why not extend it to arrays.

The thing to remember is that everything is an object, and that it's
better to think of variables as labels on an object.

So: a=b=c=7 means that _one_ integer object with the value of 7 can be
referenced using any of the labels a, b or c. x=y=z=[] means that
_one_ empty list can be referenced using x, y or z.

The difference is that the value of a number object _cannot be
changed_ ('immutable') while a list can be modified to add or remove
items ('mutable'). a=10 just reassigns the label a to an integer
object of value 10. x.append("foo") _modifies_ the list referred to by
x, which is the same list known as y & z.

Also, is there a more pythonic way to do "x=[], y=[], z=[]"?

I'd say that _is_ the most pythonic way, it's very obvious in its
intent (or would be with appropriate names). If it bothers you that
much:

def listgen(count, default=[]):
for _ in xrange(count):
yield default[:]

x, y, z = listgen(3)

.



Relevant Pages

  • Re: what does a=b=c=[] do
    ... so why not extend it to arrays. ... better to think of variables as labels on an object. ... I only use python3 so I don't know how these things work in other ... No problem in your function since you yield a copy, ...
    (comp.lang.python)
  • Re: Last Ninja Programming Language
    ... So when 88 unique variables just aren't enough (not including arrays). ... than just two character variable names. ... ``GOSUB MoveEnemy`` is much clearer than ``GOSUB 2770`` and ... labels that "compiled" such a program into compact BASIC V2. ...
    (comp.sys.cbm)
  • Re: placing labels atop single-column arrays
    ... vertically, all enclosed by brackets. ... Now I need to label these arrays: for example, ... Using the AMS align environments doesn't seem to help. ... the labels to horisontally line up. ...
    (comp.text.tex)
  • Re: Assembler: Symbolischer Zugriff auf Structs auf Heap/Stack
    ... hantieren fast nur mit skalaren Daten und Arrays davon. ... handgeschrieben und mit Labels gesehen. ...
    (de.comp.os.unix.programming)
  • Re: How to ADD LABELS to a Windows Forms APP
    ... out of the cicle... ... arrays as paramaters ... Best regards, ... I need to add several labels to a form programmatically. ...
    (microsoft.public.dotnet.csharp.general)