What's wrong with this code?

Thomas Rachel nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de
Tue Jul 24 10:34:27 CEST 2012


Am 23.07.2012 16:50 schrieb Stone Li:
> I'm totally confused by this code:
>
> Code:
>
>     a = None
>     b = None
>     c = None
>     d = None
>     x = [[a,b],
>           [c,d]]
>     e,f = x[1]
>     print e,f
>     c = 1
>     d = 2
>     print e,f
>     e = 1
>     f = 2
>     print c,d
>
> Output:
>
>     None None
>     None None
>     1 2
>
>
> I'm expecting the code as:
>
>     None None
>     1 2
>     1 2
>
>
> What's wrong?

Your expectation :-)

With c = 1 and d = 2 you do not change the respective objects, but you 
assign other objects to the same names.

The old content is still contained in x[1].

If you would only modify these objects (not possible as ints are 
immutable), you would notice the changes here and there.


Thomas



More information about the Python-list mailing list