I am trying to execute javascript code in python, using pyv8
safely. At the end of the day, I have an object being used by javascript, with few fields I would like to have hidden.
I know python doesn't have encapsulation (as explained in this question), but yet, is there a way to disable access using __getattribute__
?
class Context(object):
def __init__(self, debug):
self._a = ...
self._b = ...
self._c = ...
self._unlocked = False
def __enter__(self):
self._unlocked = True
def __exit__(self, exc_type, exc_val, exc_tb):
self._unlocked = False
def __getattribute__(self, name):
if object.__getattribute__(self, "_unlocked"):
return object.__getattribute__(self, name)
if name.startswith("_"):
return None
return object.__getattribute__(self, name)
So this object denies access to a "private" variables, unless unlocked using like this:
ctx = Context()
...
with ctx:
# _b is accessible here
print ctx._b
As far as there's no way to do with
from javascript, nor to call __enter__
since the object is "locked".
Seems not very efficient though. Is there a better way?