const in Python
Fredrik Lundh
effbot at telia.com
Tue Feb 8 05:04:35 EST 2000
Anders M Eriksson <anders.eriksson at morateknikutveckling.se> wrote:
>> > If you are truly concerned, you could make them attributes of
>>> a class that prohibits setattr. But generally naming
>
> >Right, like this:
> >
> >class ReadOnly:
> > def __setattr__(self, name, value):
> > if self.__dict__.has_key(name):
> > raise TypeError, 'value is read only'
> > self.__dict__[name] = value
> >
> >>>> const = ReadOnly()
> >>>> const.x = 5
> >>>> const.x = 9
> >TypeError: value is read only
>
> Thank You! I love your class and it will from now be in every program
> I make.
so you don't trust yourself enough to avoid overwrite
anything using a certain name convention (UPPERCASE
is pretty much standard in Python land), but are 100%
convinced you won't ever overwrite the 'const' variable?
interesting ;-)
</F>
More information about the Python-list
mailing list