Counterintuitive Python behavior

dominikush at yahoo.com dominikush at yahoo.com
Wed Apr 17 10:03:30 EDT 2002


> > Who is wrong here: my intuition or Python (2.2)? If it's
> > my intuition, how can I train my thinking about Python's
> > execution model, so that my intuition get's better ;-)
> 
> Your intuition!
> 
> Objects like lists gets copied by reference. So Python behaves exactly
> like it should.
> 
> Maybe it is clearer with another notation:
> 
>  >>> dict = {'a':<list ref1>,'b':<list ref2>}
>  >>> list = dict.values()
>  >>> list
> 
> [<list ref1>, <list ref2>]
> 
>  >>> dict['a'].append(3)
>  >>> dict
> 
> {'a': <list ref1>, 'b': <list ref2>}
> 
>  >>> list
> 
> [<list ref1>, <list ref2>]
> 
> regards Max M

I understand that! But my main objection remains: Why should
I care about the type of object being handled by a dictionary?
This could not only break unit tests but also make maintenance
of ``old'' code somewhat painful.

Regards,

Dominikus



More information about the Python-list mailing list