访问python中函数内的函数

Eiy*_*uyf 2 python

给定函数内的函数,如何从外部函数调用内部函数?

恩.

def a():
    print 'a'
    def b():
        print 'b'
        def c():
            print 'c'

def d():
    # how would I now call a, b and c from here?
    def e():
        # how would I call a, b and c from here as well?
Run Code Online (Sandbox Code Playgroud)

是的我知道这是一个糟糕的代码结构,不应该这样做 - 但是你怎么做的?

谢谢

编辑:使用装饰器的任何方式吗?

Bre*_*arn 7

你不能. b并且c是局部变量a,除了a正在执行时不存在.

(因为这些是常量,你可以在技术上通过它们访问它们a.__code__.co_consts,但即使你对可怕的代码结构没有问题,这也不是一个真正的解决方案.你必须执行函数,exec你不能将参数传递给它.)