0

How do I use inspection to retrieve a list of sub-functions?

def f( x, y):

    def g( a ):
       return a + 1

    def h( z ):
       return b + 1

subs = inspect.getsubfunctions( f ) 
#Should return [ g, h ] 
Quant
  • 4,253
  • 3
  • 17
  • 15
  • Interesting approach in the dupe, but it's prone to false positives. Lambdas, genexps, and classes all show up as code objects in the function's `func_code.co_consts`. It'll give decent results as a heuristic, still, especially for the asker's use case in the dupe. – user2357112 Mar 13 '14 at 20:38

1 Answers1

2

You can't. The "subfunctions" don't exist until the function is called. Even once it's called, the g and h definitions create new, mostly-identical functions on each f call, so you can't get the g function or the h function.

user2357112
  • 260,549
  • 28
  • 431
  • 505