Python private instance data revisited

If you just want to access the original, it's not that hard, since Python function implement a rather thorough inspection api. You can access the original secret with something like this:

thing = Secret("gibberish")
# __init__ doesn't need to be used here; anything defined within the closure will do
thing.__init__.__func__.__closure__[0].cell_contents

And, hey! We get the original value.

It is harder---but not impossible---to modify the value (see here). Modified for this setup:

import ctypes
...

thing = Secret("gibberish")
cell = ctypes.py_object(thing.__init__.__func__.__closure__[0])
new_value = ctypes.py_object('whatever')
ctypes.pythonapi.PyCell_Set(cell, new_value)

thing.getSecret()

You wouldn't ordinarily do this but you can dig into the instance with module inspect.

>>> thing = Secret("gibberish")
>>> thing.getSecret()
'tvoorevfu'
>>> import inspect
>>> inspect.getclosurevars(thing.getSecret).nonlocals['secret_data']
'gibberish'
>>> inspect.getclosurevars(thing.__init__).nonlocals['secret_data']
'gibberish'

Given one of the functions within the closure, you can access the closure's variables. I haven't yet found a way to modify the variable.

So it's not impossible if you are willing to go to some effort. Why you would do that in the normal course of programming I don't know.