nested - python - list all inner functions of a function? -


In the python you can fname .__ code __. To retrieve the list of co_names function and global items that reference a function if I fname .__ code __. Co_varnames , it contains internal functions, I believe.

Essentially the internal .__ code __. Co_names ? 'inner' looks like starting with a string, as co_vara name

I do not think you can inspect the code object because the internal functions are lazy, and their code-objects are created only in time. You probably want to see the ast module. This is a quick example:

, check # this test is the scenario def function 1 (): f1_var1 = 42 def function 2 (): f2_var1 = 42 f2_var2 = 42 def function 3 (): F3_var1 = 42 # Get the source code for the top-level function src = inspect.getsource (function1) # top-level function node = ast.parse (src) # Substitution Syntax Tree # All of the difficulty of the method is out of the tree in Ast.walk (node) for our tree-traversal: #functions are named, whereas Variables have IDs, #Nested-Class can use all different terminology # You should see different node types at # if name_or_id: print name_or_id

are the result: function1, function2 , F1_var1, very correct name _ or_id = getattr (x, 'name', getattr (x, 'id', none) function 3, f2_war 1, f2_w2, f3_war 1. compulsory disclaimer: There is no good reason to talk .. but have fun :)

Oh, and if you only have internal functions Want a name?

  print dictionary (in ast.walk for x [x, name] x (ast.parse (inspect.getsource (some_function))) if type (x) .__ name__ == 'FunctionDef'])  

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -