给定函数中的函数,如何从外部函数调用内部函数?
例如
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?
是的,我知道这是可怕的代码结构,不应该这样做 - 但你是怎么做到的?
编辑:有什么方法可以使用装饰器做到这一点?
最佳答案
你不能。 b
和 c
是 a
中的局部变量,除非 a
正在执行,否则不存在。
(因为这些是常量,从技术上讲,您可以通过 a.__code__.co_consts
访问它们,但这不是真正的解决方案,即使您可以接受糟糕的代码结构。您将不得不使用 exec
执行函数,你不能向它传递参数。)
关于python - 在 python 函数中访问函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12032587/