If I have a function f(x)
and variable var
of any type and scope is there any way to modify var
inside call to f(var)
?
Meaning that function f
does some magic stuff to get reference to original (passed) var
(like reference/pointer in C++
) and modifies that original var
, for any type of var
(even int
/float
/str
/bytes
).
Wrapping var
into dict
or list
or any other class is not allowed. Because it is known that dict/list are passed by reference.
Returning new value from a function to re-assign variable is not allowed too.
Modifying scope of original variable (like making it global) is not allowed too.
In fact any change to function's caller's code is not allowed.
This variable can be of any imaginary type and scope, no assumptions should be done about them.
So that next code should work (such code can be placed both inside another wrapping function or globally, should not matter):
def test():
def magic_inplace_add(x, val):
# ...
var = 111
magic_inplace_add(var, 222) # modifies int var by adding 222
print(var) # prints 333
test()
If needed to solve the task this function can do any complex manipulations like using inspect
module.
Basically, I need somehow to break a convention about simple types being passed by value, even if to achieve this in some non-simple/cryptic way.
I'm pretty sure this task can be solved by tools from standard reverse-engineering modules like inspect / ast / dis.
Just to clarify my need for this task - right now for this task I'm not trying to code nicely using tidy Python style and methods, you can imagine that this task in future is a kind of interview question for companies specializing in reverse-engineering / antiviruses / code security / compilers implementation. Also tasks like this are interesting in exploring all hidden possibilities of languages like Python.
as @rcvaram suggested use global variables, like this