I don' Penso che sia possibile ispezionare l'oggetto codice perché le funzioni interne sono pigre e i loro oggetti codice sono solo c battuto appena in tempo. Quello che probabilmente vorrai guardare è invece il modulo ast. Ecco un rapido esempio:
import ast, inspect
# this is the test scenario
def function1():
f1_var1 = 42
def function2():
f2_var1 = 42
f2_var2 = 42
def function3():
f3_var1 = 42
# derive source code for top-level function
src = inspect.getsource(function1)
# derive abstract syntax tree rooted at top-level function
node = ast.parse(src)
# next, ast's walk method takes all the difficulty out of tree-traversal for us
for x in ast.walk(node):
# functions have names whereas variables have ids,
# nested-classes may all use different terminology
# you'll have to look at the various node-types to
# get this part exactly right
name_or_id = getattr(x,'name', getattr(x,'id',None))
if name_or_id:
print name_or_id
I risultati sono: funzione1, function2, f1_var1, funzione3, f2_var1, f2_var2, f3_var1. Disclaimer obbligatorio: probabilmente non c'è una buona ragione per fare questo tipo di cose .. ma divertiti :)
Oh e se vuoi solo i nomi delle funzioni interne?
print dict([[x.name,x] for x in ast.walk(ast.parse(inspect.getsource(some_function))) if type(x).__name__=='FunctionDef'])
Si tratta di una cosa di un livello o di funzioni nidificate? –