如何通过用户创建的配置文件动态设置参数默认值?在
配置文件由声明了参数的函数路径组成。能够导入函数将允许我获得函数名及其模块路径。在
有没有人知道如何导入一个给定了函数行数的函数以及包含该函数的python文件(包括类中的函数)?在
编辑(根据评论的要求添加了以下用例)
我想在下面的示例中检索默认参数为configured_arg()的函数:class SomeClass:
def __init__(kwarg=configured_arg()):
...
def some_function(kwarg=configured_arg()):
...
def another_function(kwarg=configured_arg()):
...
def configured_arg():
# NOTE: `inspect.stack` returns the caller as ""
# NOTE: `inspect.stack` returns the `lineno` and file path of the call
caller = get_caller() # Unsure of how to implement
arg_key = get_arg_key() # Unsure of how to implement
return fetch_configured_value(caller, arg_key) # Fetch default arg from global config
在发布SO之前,我尝试使用inspect.stack。调用方(例如some_function)不出现在configurable_arg的堆栈中。但是,inspect.stack具有lineno默认参数已声明。这一经历启发了当前的问题。在