Given the python function:
def MyPythonMethod(value1, value2):
# defining some variables
a = 4
myValue = 15.65
listValues = [4, 67, 83, -23]
# doing some operation on the list
listValues[0] = listValues[1]
# looping through the values
for i in listValues:
print i
How can I extract the names
and types
of all the variables in method MyPythonMethod
?
Ideally, I'd like to get all variable names and their ty开发者_开发问答pes given a method name. for example, the output for method MyPythonMethod
will look like this:
varNames = ["a", "myValue", "listValues", "i"]
varTypes = ["int", "float", "list", "float"]
Any ideas?
1 Variables don't have a type in python. Objects have a type, and variables point to objects.
[2] you can use the inspect module to get info about the internals of your function. Read the docs -- they will tell you what is available for inspection. MyPythonMethod.func_code.co_varnames will give you the local variable names, for example. ( And note that MyPythonMethod, as defined, is actually a function, not a method. )
[3] But even when you get the names of the local variables, the aren't bound to any objects except while the function is executing. The value 4 is bound to local var 'a' in the function -- before and after the function is called, there is no 'a' and it's not bound to anything.
[4] If you run the function in the debugger, you can halt the execution at any point and inspect the variables and objects created in the function.
[5] If the function raises an exception, you can catch the exception and get access to some of the state of the function at the time of the exception.
You can't do this "from the outside".
- Local variables don't exist until the method runs. Although the scope of all variables is known statically, i.e. at compiletime, I don't think you can get this information easily without crawling through the AST or bytecode yourself. (Edit: Steven proved me wrong about this one... code objects have a tuple containing all local variable names)
- A given chunk of code doesn't have access to any scopes but its own and the sourrounding "lexical" scopes (builtins, module-level globals, local scopes of enclosing functions).
- There is no such thing as the type of a variable (in Python) - any variable can refer to any number of objects of completely different types during its lifetime. What should the output be if you add
a = "foo"
? And if you then adda = SomeClass()
?
Inside the method itself, you could use locals()
to get a dictionary of local variables and the objects they currently refer to, and you could proceed to call type
on the values (the objects). Of course this only gets you the type of the object currently referred to. As hinted in the comment, I doubt that this is useful. What do you really want to do, i.e. what problem are you trying to solve?
If you use pdb
can't you set the last line as a breakpoint and then ask the debugger to look at the top stack frame and list the variables for you? Or you could look at the pdb
code and copy its tricks for how to introduce the breakpoint and then inspect the stack frame beneath the breakpoint function that you register.
精彩评论