python copy method alters type

Wynand Winterbach wynand.winterbach at gmail.com
Thu May 14 15:12:13 CEST 2009


My only guess is that the code below is not using the copy function from
the copy module, but that some other function is aliasing the copy
function.

I've tried this code with both Python 2.5 and 2.6 and get the correct
behaviour.

Best
Wynand

Op donderdag 14-05-2009 om 09:05 uur [tijdzone -0400], schreef Zhenhai
Zhang:
> Really weired; Here is my code:
> 
>     a = ["a", 1, 3, 4]
>     print "a:", a
>    
>     c = copy(a)
>     c[0] = "c"
>     c[1] = 2
>     print "c:", c
>     print "a:",a
> 
> output as follows:
> 
> a: ['a', 1, 3, 4]
> c: ['c' '2' '3' '4']
> a: ['a', 1, 3, 4]
> 
> Btw, I'm using python 2.5. I'm very curious why the copied list changed 
> data type.
> 
> Thanks,
> 
> Zhenhai




More information about the Python-list mailing list