Python has a "really hidden encapsulation"?
Arnaud Delobelle
arnodel at gmail.com
Sat Oct 23 14:51:59 EDT 2010
dmytro starosud <d.starosud at gmail.com> writes:
>
> I think I'm being realized that Python allows to do everything.
> Maybe I will not try to find "really hidden encapsulation". :)
I think it's a wise decision :)
Just to challenge you a bit, here is another (doomed) attempt at having
private attributes for object instances:
def private_maker():
class Private: pass
privmap = {}
def private(f):
def wrapper(self, *args, **kwargs):
priv = privmap.setdefault(self, Private())
return f(self, priv, *args, **kwargs)
return wrapper
return private
private = private_maker()
class A:
@private
def __init__(self, private, x):
private.x = x
@property
@private
def x(self, private):
return private.x
del private
a = A(2)
Can you change the value of a.x?
(Hint: my shortest solution is of the form A.*.*[*].*[*].x = 3)
> p.s. what do you think about the presence of two fields v_min (in
> first message):
> “s.__dict__['v_min']” and “s.v_min”?
Are you referring to the fact that in Python, if an attribute is a
property, the __dict__ lookup on the instance is not performed? As in:
>>> class A:
... @property
... def x(self): return 42
...
>>> a = A()
>>> a.__dict__['x'] = 24
>>> a.x
42
>>> a.__dict__['x']
24
This is documented, but I actually don't know the reason for it.
--
Arnaud
More information about the Python-list
mailing list