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

Steven D'Aprano steve at pearwood.info
Sun Feb 26 00:05:56 CET 2012

Ned Batchelder wrote:

> The Python answer for people who want read-only data structures has 
> always been, "Don't modify them if you don't want to, and write docs 
> that tell other people not to as well."  What are you building that this 
> answer isn't good enough?

That is silly. That alleged "Python answer" is like telling people that they 
don't need test frameworks or debuggers because the "Python answer" for people 
wanting to debug their code is not to write buggy code in the first place.

Python has read-only data structures: tuple, frozenset, str, etc. If you ask 
yourself why Python has immutable types, it might give you a clue why Victor 
wants the ability to create other immutable types like frozendict, and why 
"don't modify them" is not a good enough answer:

- Immutable types can be used as keys in dicts.

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

Immutability gives you an immediate exception at the exact time and place you 
attempt to modify the data structure instead of at some arbitrary time later 
far from the actual bug.

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


More information about the Python-ideas mailing list