Is there a commas-in-between idiom?

Peter van Kampen news at p2b.datatailors.com
Thu Nov 9 06:31:11 EST 2006


On 2006-11-08, Georg Brandl <g.brandl-nospam at gmx.net> wrote:
> Peter van Kampen schrieb:
>> On 2006-11-06, Fredrik Lundh <fredrik at pythonware.com> wrote:
>>> I've collected a bunch of list pydioms and other notes here:
>>>
>>>     http://effbot.org/zone/python-list.htm
>> 
>> """
>> A = B = [] # both names will point to the same list
>> """
>> 
>> I've been bitten by this once or twice in the past, but I have always
>> wondered what it was useful for? Can anybody enlighten me?
>
> Do you never have a situation where you want to assign the same value
> to two variables?

In the sense that I might want two empty lists or maybe 2 ints that
are initialised as 0 (zero). That's what I (incorrectly) thought A = B
= [] did.

> Or are you objecting to the fact that both names point to the same
> object?

I'm not objecting to anything, merely wondering when I could/should
use this idiom. There's a nice example in another response (self.items
= items = [])

> It couldn't be otherwise. Consider:
>
> X = []
> A = B = X
>
> What should this do? Copy "X" and assign one copy to A, one to B?

Ah yes, I see the error in my ways...I skipped the A = B part too
lightly.

Thanks,

PterK


-- 
Peter van Kampen
pterk -- at -- datatailors.com



More information about the Python-list mailing list