Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get/set local variables of a function (from outside) in Python?

Tags:

python

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:

  1. How to get the local variables (a,b) of the function from outside without using locals() inside the function? (kind of reflection)
  2. 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:

  1. I have an encrypted python source file containing a python function.
  2. A C extension module decrypts it and builds that function in-memory.
  3. A main python program first calls the C extension with that encrypted file location.
  4. Then the main program calls the function that has been built in-memory (by the C extension)
  5. But main program needs to know the local variables of that function (Dont ask me why, it was not me)
  6. For some (damn) reason, main program needs to set a variable too (weirdest of all)
like image 498
mshsayem Avatar asked Sep 01 '09 05:09

mshsayem


People also ask

How do you access local variables outside a function in Python?

Using the global statement If you want to assign a value to a name defined outside the function, then you have to tell Python that the name is not local, but it is global. We do this using the global statement. It is impossible to assign a value to a variable defined outside a function without the global statement.

How do you access local variables outside class?

If you want to use that variable even outside the class, you must declared that variable as a global. Then the variable can be accessed using its name inside and outside the class and not using the instance of the class.

Can we access variable outside function in Python?

In Python, a variable declared outside of the function or in global scope is known as a global variable. This means that a global variable can be accessed inside or outside of the function.

Can a local variable be used outside a function?

A local variable is defined inside a function or a block statement and is not accessible outside the function or block. This means that these variables are visible and can only be used by the function in which the variable is defined!


2 Answers

No. A function that isn't being run doesn't have locals; it's just a function. Asking how to modify a function's locals when it's not running is like asking how to modify a program's heap when it's not running.

You can modify constants, though, if you really want to.

def func():
    a = 10
    print a

co = func.func_code
modified_consts = list(co.co_consts)
for idx, val in enumerate(modified_consts):
    if modified_consts[idx] == 10: modified_consts[idx] = 15

modified_consts = tuple(modified_consts)

import types
modified_code = types.CodeType(co.co_argcount, co.co_nlocals, co.co_stacksize, co.co_flags, co.co_code, modified_consts, co.co_names, co.co_varnames, co.co_filename, co.co_name, co.co_firstlineno, co.co_lnotab)
modified_func = types.FunctionType(modified_code, func.func_globals)
# 15:
modified_func()

It's a hack, because there's no way to know which constant in co.co_consts is which; this uses a sentinel value to figure it out. Depending on whether you can constrain your use cases enough, that might be enough.

like image 123
Glenn Maynard Avatar answered Sep 17 '22 12:09

Glenn Maynard


The function's locals change whenever the function is run, so there's little meaning to access them while the function isn't running.

like image 37
orip Avatar answered Sep 18 '22 12:09

orip