2.3 list reverse() bug?

Bjorn Pettersen bjorn.pettersen at comcast.net
Thu Dec 25 06:22:26 EST 2003


Robin Becker <robin at jessikat.fsnet.co.uk> wrote in
news:Mo0idJAPes6$EwWj at jessikat.fsnet.co.uk: 

> In article <d3c9c04.0312250303.561b119d at posting.google.com>, Mark
> Carter <cartermark46 at ukmail.com> writes
>>I did this:
>>
>>Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on
>>win32
>>
>>>>> d1 = [1,2]
>>>>> d2 = d1
>>>>> d2.reverse()
>>>>> print d1 #note: d1, not d2
>>[2, 1]
>>>>> 
>>
>>Surely that can't be right: d1 should still be [1,2]. If it is
>>"right", then I expect that many people are in for a suprise.
> Really need to get a life, but anyhow here goes. 
> 
> It's right. d1 & d2 both point to the same mutable object and sort is
> done in place.
> 
> compare with this
> 
>>>> d1=[1,2]
>>>> d2=d1
>>>> d2[0]='a'
>>>> d1['a', 2]
>>>> 

that's probably less confusing when adding the hidden newline:

>>> d1=[1,2]
>>> d2=d1
>>> d2[0]='a'
>>> d1
['a', 2]
>>>

it's-4:30am-here'ly y'rs
-- bjorn





More information about the Python-list mailing list