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



alex23 schreef op wo 21-12-2011 om 16:50 [-0800]:
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)



I'm afraid it's dangerous to encourage the use of '[]' as assignment to
a parameter in a function definition. If you use the function several
times 'default' always points to the same list.

>>> def return_list(list_ = []):
>>> return list_
>>> a_list = return_list()
>>> a_list
[]
>>> a_list.append(3)
>>> a_list
[3]
>>> b_list = return_list()
>>> b_list
>>> [3] # !!??

>>> def return_list():
>>> return []
>>> a_list = return_list()
>>> a_list
[]
>>> a_list.append(3)
>>> a_list
[3]
>>> b_list = return_list()
>>> b_list
>>> [] # OK!

I only use python3 so I don't know how these things work in other
versions.

No problem in your function since you yield a copy, but I've already
seen long threads about this.

I would change your function to (Python3.x):

def empty_lists(count):
for _ in range(count):
yield []


Regards,

Rolf




.



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. ... yield default ...
    (comp.lang.python)
  • Re: About regaining the memory associated with a block-specific variable and reusing it further.
    ... of reasonable sizes specific to blocks. ... This is unlikely to yield a good return on your time. ... Peter Seebach / usenet-nospam@xxxxxxxxx ...
    (comp.lang.c)
  • Re: RE
    ... Comment me if there is any mistake of mine please? ... Even the @weekdays doen't yield all the days instead it yields the combination of three days... ... I was originally going to have arrays that I joined to make ... scalars that I was going to put into the regex, but I got lazy 'cause ...
    (perl.beginners)
  • Re: A small problem for arrays
    ... if Arrays are large and / or there are frequent set ... operations needed then using class Set might yield better performance. ... the intersection of two Sets is ...
    (comp.lang.ruby)
  • Re: A small problem for arrays
    ... if Arrays are large and / or there are frequent set ... operations needed then using class Set might yield better performance. ... the intersection of two Sets is ...
    (comp.lang.ruby)