Favorite non-python language trick?

Robert Kern rkern at ucsd.edu
Mon Jun 27 07:47:04 EDT 2005


Steven D'Aprano wrote:
> On Sun, 26 Jun 2005 23:22:00 -0500, Terry Hancock wrote:
> 
>>>You need to differentiate
>>>   a = b = 1
>>>from
>>>   a = b == 1
>>
>>Okay, I see what you mean.  I can't ever recall having needed the
>>second form, though.
>>
>>Of course, you could still do assignment like this:
>>
>>a, b = (1,)*2
>>
>>But I guess that's not exactly elegant. ;-)
> 
> In general that is not the same thing as a = b = obj.
> 
> py> a, b = ([], [])
> py> a.append(1)
> py> b
> []
> py> a = b = []
> py> a.append(1)
> py> b
> [1]

What you wrote isn't, but what Terry wrote is.

In [1]: a, b = ([],)*2

In [2]: a.append(1)

In [3]: b
Out[3]: [1]

In [4]: a is b
Out[4]: True

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter




More information about the Python-list mailing list