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

Victor Stinner victor.stinner at haypocalc.com
Sat Feb 25 03:29:12 CET 2012

Hum, after thinking twice, using a "frozendict" for type.__dict__ is
maybe overkill for my needs (and intrused as noticed Nick). Attached
patch for Python 3.3 is a simpler approach: add __final__ special
value for class. If this variable is present, the type is constant.
class Test:
   x = 1

Test.x = 2 # raise a TypeError
Test.new_attr = 1 # raise a TypeError
del Test.x # raise a TypeError

There are various ways to deny the modification of a class attribute,
but I don't know how to block the removal of an attribute of the
addition of a new attribute without my patch.


My patch is just a proof-of-concept. For example, it doesn't ensure
that values are read-only too. By the way, how can I check that "a
value is constant"? Except builtin immutable types, I suppose that the
only way is to call hash(obj) and excepts an expect a TypeError.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: type_final.patch
Type: text/x-patch
Size: 2345 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120225/3e4c8fba/attachment.bin>

More information about the Python-ideas mailing list