[Python-ideas] Support other dict types for type.__dict__
Victor Stinner
victor.stinner at haypocalc.com
Fri Feb 24 01:27:37 CET 2012
> And you can't use __slots__ because...?
Hum, here is an example:
---
def Enum(**kw):
class _Enum(object):
__slots__ = list(kw.keys())
def __new__(cls, **kw):
inst = object.__new__(cls)
for key, value in kw.items():
setattr(inst, key, value)
return inst
return _Enum(**kw)
components = Enum(red=0, green=1, blue=2)
print(components.red)
components.red=2
print(components.red)
components.unknown=10
---
components.unknown=10 raises an error, but not components.red=2.
__slots__ denies to add new attributes, but not to modify existing
attributes.
The idea of using a frozendict is to deny the modification of an
attribute value after the creation of the object. I don't see how to
use __slots__ to implement such constraints.
Victor
More information about the Python-ideas
mailing list