Gathering variable names from within a function.

X

Xavier

Greetings,

I was wondering if there was a way to get a list of all the variables which
were executed/created/modified within a function?

Here is a simple example that comes to my mind:

def getVars(func):
...
...
...

def modVar(arg, arg2):

Var1 = arg
Var2 = arg2
Var3 = arg+arg2
return Var3

def main():

print getVars(modVar('test', 'test2'))

# end

naturally, "Var1", "Var2" and "Var3" should be printed to the user. Any
idea?

-- Xavier

oderint dum metuant
 
P

Peter Hansen

Xavier said:
I was wondering if there was a way to get a list of all the variables which
were executed/created/modified within a function?

Have you looked at the "inspect" standard module?
 
M

Michele Simionato

Xavier said:
Greetings,

I was wondering if there was a way to get a list of all the variables which
were executed/created/modified within a function?

Here is a simple example that comes to my mind:

def getVars(func):
...
...
...

def modVar(arg, arg2):

Var1 = arg
Var2 = arg2
Var3 = arg+arg2
return Var3

def main():

print getVars(modVar('test', 'test2'))

# end

naturally, "Var1", "Var2" and "Var3" should be printed to the user. Any
idea?

What about

def modVar(arg, arg2):
Var1 = arg
Var2 = arg2
Var3 = arg+arg2
print vars()
return Var3

modVar('test', 'test2')

=>

{'arg2': 'test2',
'arg': 'test',
'Var1': 'test',
'Var3': 'testtest2',
'Var2': 'test2'}
-- Xavier

oderint dum metuant

Michele
 
S

Sean Ross

This will seem excessive, but it`s a little hack I`ve been playing with.
WARNING: It`s incomplete, and, in general, it should probably *not* be
used. Still, here it is, if you`re interested:

import inspect

class ThisResolutionError(Exception):
pass

def this():
"returns the function object that calls it"
# get calling function's name from outer frame
fname = inspect.currentframe(1).f_code.co_name
frameno = 2
func = None
while func is None:
# search the outer frames for a reference to this function
try:
func = inspect.currentframe(frameno).f_locals.get(fname, None)
frameno += 1
except ValueError:
# reached end of call stack
raise ThisResolutionError, "could not resolve %s"%fname
return func # return this function object

'this()' can be used to get hold of the function that you're currently in;
you may then manipulate or inspect that function as you please.

for example:

def modvars(arg1, arg2):
"records it's own modified variables"
var1 = arg1
var2 = arg2
var3 = arg1+arg2
this().__dict__ = vars()
print this().__doc__
return var3

modvars(1,2)
print modvars.__dict__

# OUTPUT:
# records it's own modified variables
# {'arg1': 1, 'arg2': 2, 'var1': 1, 'var3': 3, 'var2': 2}


'this()' appears to work for functions, including nested functions, but it
does not work for bound/unbound methods, or functions stored in a dictionary
(yet). For instance, you can do the following:

def f():
def g():
print "g says 'Hello'"
this().g = g
print f.g
print f.g()
# <function g at 0x00958C60>
# g says 'Hello'

But you can't do this:

def f():
def g():
"g says 'Hello'"
print this().__doc__
this().g = g
print f.g()
# ThisResolutionError: could not resolve g

Anyway, it's still pretty neat to be able to grab hold of a function, from
inside itself, and work with it.
Sean
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top