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

David Townshend aquavitae69 at gmail.com
Sun Feb 26 15:30:09 CET 2012


Ah, I think I misunderstood exactly what you were trying to achieve.  To
me, that is essentially immutable - if I ever found myself using
type.__setattr__ to change a variable I'd have to seriously question what I
was doing!  But that would be a way around it, and I don't think it would
be possible to implement it fully in python.  On the other hand, the same
argument could be made for the introduction of private variables;
Class.__var is not private because it can be changed through
Class._Class__var.  I'd also consider having to do this to be indicative of
a design flaw in my code.

On Sun, Feb 26, 2012 at 3:56 PM, Victor Stinner <
victor.stinner at haypocalc.com> wrote:

> 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
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120226/9da7f19e/attachment.html>


More information about the Python-ideas mailing list