encapsulation?
chris liechti
cliechti at mails.ch
Tue Aug 7 17:13:20 EDT 2001
Lloyd Goldwasser <goldwasser at demog.berkeley.edu> wrote in
news:3B704ED1.EFA6AEA8 at demog.berkeley.edu:
> How hard (or desirable) would it be to provide a stronger kind
> of encapsulation in Python?
the posibility to access any value even pyrivate ones is useful for
debugging. if there was an accsess control it must also let pass a debugger
but that doesn't sound secure too - any python programm could then pretend
to be a debuger and get the data.
there is also a performance impact when *every* object has to be checked
(at runtime) if it can be accessed from the current function.
and i think its like other things e.g. operator overloading: its left out
in java because you can do bad things with it (like how do you multiply two
mail addresses?) but you can do it in python because its essential if you
want to have first-class user objects.
access control for attributes prvents an unexpirenced programmer or one who
doesn't know the object he's trying to use, from modifying critical values.
on the other hand the experienced user will be restricted by those means.
how hard it would be to add it to python? maybe one could programm a
special wrapper (proxy pattern?). a class with the name "protected" which
evalutaes the call stack for each accessed attribute and decide uppon that
if the caller is a friend or not (based on namespaces?).
to make an attribute protected on would need to wrap it in the __init__:
class A:
def __init__(self):
somevar = "init this one"
self.prot = protected(somevar) #save it with the wrapper
don't know if thats possible, i've never played around with those things
but the exceptions know the call stack too.
--
chris <cliechti at mails.ch>
More information about the Python-list
mailing list