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

Victor Stinner victor.stinner at haypocalc.com
Sun Feb 26 14:56:08 CET 2012


type.__setattr__(Three, 'value', 4) changes the value.

Victor

> class FinalMeta(type):
>
>     def __setattr__(cls, attr, value):
>         if attr in cls.__dict__ or '__done__' in cls.__dict__:
>             raise AttributeError
>         else:
>             type.__setattr__(cls, attr, value)
>
>     def __delattr__(cls, attr):
>         raise AttributeError
>
>
> class Three:
>     __metaclass__ = FinalMeta
>     value = 3
>     __done__ = True   # There may be a neater way to do this...
>
> Each of the following examples will fail:
>
>>>> Three.another_value = 4
>>>> Three.value = 4
>>>> del Three.value
>>>> three = Three(); three.value = 4



More information about the Python-ideas mailing list