getting name of passed reference
Francesco Bochicchio
bieffe62 at gmail.com
Tue Dec 29 10:43:44 EST 2009
On 29 Dic, 00:54, Joel Davis <callmeclaud... at gmail.com> wrote:
> I'm just curious if anyone knows of a way to get the variable name of
> a reference passed to the function.
>
> Put another way, in the example:
>
> def MyFunc ( varPassed ):
> print varPassed;
>
> MyFunc(nwVar)
>
> how would I get the string "nwVar" from inside of "MyFunc"? is it
> possible?
The following code shows one way to get both function name and
argument names from inside a function using module inspect in python
2.6:
import inspect
def myfunc(arg1, arg2):
f = inspect.currentframe()
funcname = inspect.getframeinfo(f).function
numargs = f.f_code.co_argcount
argnames = f.f_code.co_varnames[:numargs]
print funcname, argnames
myfunc(1, "ppp")
NOTE: it does not list parameters passed as list (*args) or as dict
(**kwd).
P.S . I use this to generate automatically trace messages of type
"called myfunc( arg1=1, arg2=ppp" ).
But I currently lack a way, from inside a method, to determine the
name of the class to which the
method belong, so I could automatically generate trace messages of
type "class.method called etc ...".
Pointers are welcome.
Ciao
-----
FB
More information about the Python-list
mailing list