如何从外部Python文件调用另一个文件中的嵌套内部函数?
问题根源
嵌套函数some_function的作用域仅限于外部函数one_function内部,模块的全局命名空间里不存在这个函数,所以直接通过from ... import some_function的方式肯定找不到它。
解决办法
方法1:将嵌套函数移到模块级别
直接把some_function从one_function内部提取到模块顶层,这样就能正常导入使用。如果需要用到原内部的threshold,可以把它改成模块级变量:
修改stats_file.py:
# 模块级变量,原内部的threshold移到这里 threshold = 1 # 直接定义在模块级别 def some_function(variable_1, variable_2, variable_3): outcome_text = "" # 注意:原代码中的x、y、ratio需要替换为实际从参数或其他地方获取的变量,否则会报错 if x > y > threshold: if ratio <= 0.69 * threshold: outcome_text = "text 1" elif ratio <= 0.70 * threshold: outcome_text = "text 2" elif ratio >= 0.80 * threshold: outcome_text = "text 3" elif ratio >= 0.90 * threshold: outcome_text = "text 4" else: outcome_text = "text 5" else: outcome_text = "text 6" return outcome_text def one_function(): def get_outcome_text(variable_1, variable_2, variable_3): outcome_text = some_function(variable_1, variable_2, variable_3) return outcome_text outcome_text = get_outcome_text(variable_1, variable_2, variable_3) print(outcome_text)
之后shared_data.py和loop_file.py就可以正常导入:shared_data.py:
from stats_file import some_function
loop_file.py:
from shared_data import some_function
方法2:让外部函数返回嵌套函数
如果不想改动原有嵌套结构,可以修改one_function,让它执行后返回some_function,这样导入外部函数后调用就能拿到嵌套函数:
修改stats_file.py:
def one_function(): threshold = 1 def some_function(variable_1, variable_2, variable_3): outcome_text = "" if x > y > threshold: if ratio <= 0.69 * threshold: outcome_text = "text 1" elif ratio <= 0.70 * threshold: outcome_text = "text 2" elif ratio >= 0.80 * threshold: outcome_text = "text 3" elif ratio >= 0.90 * threshold: outcome_text = "text 4" else: outcome_text = "text 5" else: outcome_text = "text 6" return outcome_text def get_outcome_text(variable_1, variable_2, variable_3): outcome_text = some_function(variable_1, variable_2, variable_3) return outcome_text outcome_text = get_outcome_text(variable_1, variable_2, variable_3) print(outcome_text) # 返回嵌套函数 return some_function
shared_data.py中转处理:
from stats_file import one_function # 调用one_function获取嵌套函数 some_function = one_function()
loop_file.py直接导入使用:
from shared_data import some_function
方法3:给外部函数绑定嵌套函数作为属性
在one_function内部,把some_function绑定为外部函数的属性,这样导入外部函数后可以通过属性访问:
修改stats_file.py:
def one_function(): threshold = 1 def some_function(variable_1, variable_2, variable_3): outcome_text = "" if x > y > threshold: if ratio <= 0.69 * threshold: outcome_text = "text 1" elif ratio <= 0.70 * threshold: outcome_text = "text 2" elif ratio >= 0.80 * threshold: outcome_text = "text 3" elif ratio >= 0.90 * threshold: outcome_text = "text 4" else: outcome_text = "text 5" else: outcome_text = "text 6" return outcome_text # 将嵌套函数绑定为外部函数的属性 one_function.some_function = some_function def get_outcome_text(variable_1, variable_2, variable_3): outcome_text = some_function(variable_1, variable_2, variable_3) return outcome_text outcome_text = get_outcome_text(variable_1, variable_2, variable_3) print(outcome_text)
shared_data.py中转处理:
from stats_file import one_function # 必须先调用一次one_function,属性才会被绑定 one_function() # 访问外部函数的属性获取嵌套函数 some_function = one_function.some_function
loop_file.py直接导入使用即可。
内容的提问来源于stack exchange,提问作者Newbie
相关产品推荐
相关产品推荐

