Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Gathering variable names from within a function.

Reply
Thread Tools

Gathering variable names from within a function.

 
 
Xavier
Guest
Posts: n/a
 
      07-04-2003
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



 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      07-04-2003
Xavier wrote:
>
> 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?
 
Reply With Quote
 
 
 
 
Michele Simionato
Guest
Posts: n/a
 
      07-05-2003
"Xavier" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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
 
Reply With Quote
 
Sean Ross
Guest
Posts: n/a
 
      07-07-2003
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamic variable names or using a variable as another's name Peter Buckley Ruby 5 02-27-2009 06:05 PM
checking for mis-spelled variable names / function names News123 Python 2 11-26-2008 12:37 AM
creating variable names within a perl script bdz Perl Misc 3 07-16-2006 10:11 PM
Gathering data from form fields within iFrames Jon Javascript 2 01-25-2006 09:51 PM
confusion between global names and instantiated object variable names wanwan Python 3 10-14-2005 09:46 PM



Advertisments