sharing dictionaries amongst class instances

Kerry Neilson kmneilso at REMOVEyahoo.com
Wed Nov 12 00:42:10 EST 2003


> See? The old value seems to reappear. When you say a.x and there is no
> instance attribute, Python looks up the class attribute with the same
name.
> So you can use the class attribute as a kind of default value for the
> instance attribute.
> But beware, this technique bites you as soon as you try it with mutables:
>
> >>> a.y["key"] = "value"
>
> This looks the same, whether y is an instance or class attribute, and you
> would not complicate client code with some detection scheme.
>
> >>> A.y
> {'key': 'value'}
> >>>
>
> Oops, there was no y attribute in the instance, so we inadvertently
changed
> y of the underlying class.

Thank you.  Your message really cleared a lot of things up for me.






More information about the Python-list mailing list