I read various "there is no really personal data in Python instances", but we all know how to use closure in Perl and JavaScript to efficiently retrieve private data. So why not Python? For instance:
import codecs class Secret: def __private(): secret_data = None def __init__(self, string): nonlocal secret_data if secret_data is None: secret_data = string def getSecret(self): return codecs.encode(secret_data, 'rot_13') return __init__, getSecret __init__, getSecret = __private()
Now we do:
>>> thing = Secret("gibberish") >>> thing.getSecret() 'tvoorevfu' >>> dir(thing) ['_Secret__private', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'getSecret']
What can you do for an instance to get read access to the original string (ignoring my weak encryption) or write access to it?
I am teaching Python lessons to my students this week, and I am trying to understand why, given the closure, the methods for JavaScript and Perl will not work for Python.
Thanks.