Constants and Globals

sismex01 at hebmex.com sismex01 at hebmex.com
Mon Dec 16 12:38:16 EST 2002


> From: Pierre Rouleau [mailto:prouleau_nospam at impathnetworks.com]
> Sent: Monday, December 16, 2002 11:22 AM
> 
> >>>>>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?
> 
> Pierre
>

It's good because it protects you from your own mistakes;
all implementation-specific attributes and methods are
normally prefixed by (at least) an underscore, so you
*know* that it shouldn't be touched except under dire
circumstances.

Now, suppose you're actually *in* one of those dire
circumstances: you have to make this program work, and
the best way to make it work is to actually *use* this
private attribute or method; so, you use it.  For the
specific versions of the modules/classes you're using,
this may be a valid proposition.

BUT, if you're using one of those languages which
enforce this private/public permissions for you, then
your up the creek, there's no way --outside of cheating
the compiler-- to access those attributes or methods.

OTOH, it also protects you from your own hubris: you
mark an attribute or method as private, and later
(after it's placed in production) you find out that
you need it in another place, public.  Hmmm... well,
let's add an accessor method here and there, and your
code get's all cludged up.

-gustavo




More information about the Python-list mailing list