How do you create constants?

Carel Fellinger cfelling at iae.nl
Sun Oct 29 11:56:10 EST 2000


Dale Strickland-Clark <dale at out-think.nospamco.uk> wrote:
...
> If you want to use 'set once' values, you could write a simple class
> for that. Something like this:

> class SetOnce:
> 	def __setattr__(self, key, value):
> 		if key in dir(self):
> 			raise ValueError(key)
> 		self.__dict__[key] = value


but be aware that if some joker would replace this __setattr__ method like:

   def noSetOnce(self,key,value:
       self.__dict__[key] = value

   SetOnce.__setattr__ = noSetOnce

then suddenly SetOnce constants aren't constant anymore.
So you're back to having to trust your co-workers:)
And yes, the same can be done for a single instance.
And yes again, thanks to type casts, similar things can be done in e.g. C++.

Hope this gets you scared again:)
-- 
groetjes, carel



More information about the Python-list mailing list