[Python-ideas] Support other dict types for type.__dict__

Masklinn masklinn at masklinn.net
Sun Feb 26 00:32:52 CET 2012


On 2012-02-26, at 00:05 , Steven D'Aprano wrote:
> - Immutable types can be used as keys in dicts.

*technically*, you can use mutable types as dict keys if you define
their __hash__ no? That is of course a bad idea when the instances
are *expected* to be modified, but it should "work".

> - Immutable types protect you from errors. While you might intend not
>  to modify a data structure, bugs do happen.

Immutables are also inherently thread-safe (since thread safety is about
shared state, and shared immutables are not state). Which is a nice
guarantee.

> Python has excellent support for read-only data structures, so long as you write them in C.

There's also good support of the "consenting adults" variety (use
_-prefixed attributes for the actual state and expose what needs to be
exposed via properties and methods). That can be simplified with a
custom descriptor type which can only be set once (similar to java's
`final`), it would be set in the type's constructor and never re-set
from this.




More information about the Python-ideas mailing list