If I have a function (in Python 2.5.2) like:
def sample_func():
a = 78
b = range(5)
#c = a + b[2] - x
My questions are:
- How to get the local variables (a,b) of the function from outside without using locals() inside the function? (kind of reflection)
- Is it possible to set a local variable (say x) from outside so that the commented line works? (I know it sounds weird).
Thanks in advance.
EDIT:
Everyone is asking for a use-case. But it is a weird situation. (Don't blame me, I did not create it). Here is the scenario:
- I have an encrypted python source file containing a python function.
- A C extension module decrypts it and builds that function in-memory.
- A main python program first calls the C extension with that encrypted file location.
- Then the main program calls the function that has been built in-memory (by the C extension)
- But main program needs to know the local variables of that function (Dont ask me why, it was not me)
- For some (damn) reason, main program needs to set a variable too (weirdest of all)