Constants and Globals

Ben Leslie benno at sesgroup.net
Mon Dec 16 17:39:22 EST 2002


On Mon, 16 Dec 2002, Pierre Rouleau wrote:

> 
> >>>>>a.version = 0
> >>>>
> >>Traceback (most recent call last):
> >>  File "<stdin>", line 1, in ?
> >>  File "protect.py", line 107, in writeAccess
> >>     raise self.AccessError, "Can not rebind attribute"
> >>AccessError: Can not rebind attribute
> >>
> >
> >>>>a.__class__.version = 3
> >>>>a.version
> >>>
> >3
> Thnaks for pointing this out.
> 
> >
> >There is no way to make a truly read only variable in python.
> >(Which is a good thing)
> And why is this a good thing?  If something is not meant to be 
> modifiable in a class, why not being able to make it constant?  Python 
> has immutable things. so why not have the ability to have constant things?

Because when someone writes a library that is almost perfectly,
but then goes an mistakenly flags something as private, which I 
need to be public, and I really really know what I'm doing, I can.

Also as soon as you mixin any C libraries, you have some code that can
touch any piece of memory it wants, so the illusion of immutable things
is well and truly blown away.

When protection is *really* needed you use the operating system's
primitives, (that is, addresses spaces), to provide it, not the language[1] 
(Of course  current OS's have very coarse grained level of protection, which 
means this sometimes sucks, but that is a discussion for comp.os.research, not
here :).


Benno


[1] Of course I'm into OS design so I'm a little bit biased :)




More information about the Python-list mailing list