提问人:Roma V 提问时间:8/14/2022 更新时间:8/14/2022 访问量:84
如果 python 中的另一个输入参数为 TRUE,有没有办法将函数的所有输入参数设置为 TRUE?
Is there a way to set all input arguments of a function to TRUE if one other input argument is TRUE in python?
问:
我有以下函数,我用它来运行分析片段。
如果我设置 ,我想同时运行analysis_1和analysis_2。如果我设置 when 是 ,我将运行 analysis_1(analysis_2也是如此)run_all_analysis = True
run_analysis_1 = True
run_all_analysis
False
def save_locally(self,
run_all_analysis: bool = False,
run_analysis_1: bool = False,
run_analysis_2: bool = False):
if run_all_analysis:
run_analysis_1 = True
run_analysis_2 = True
if run_analysis_1 = True
# some function that saves analysis 1 to local folder
if run_analysis_2 = True
# some function that saves analysis 2 to local folder
问题是,我有很多不同的分析(大约 20 个),所以这个函数可能会变得非常长。有没有办法自动将所有输入参数设置为 if is(而不是一个接一个地写出所有输入参数?TRUE
run_all_analysis
TRUE
答:
最简单的方法是使用 to take all arguments after, loop 并使用运算符设置项目。*args
or
然后使用函数设置变量。locals()
def save_locally(self,
run_all_analysis: bool = False,
*runs_analysis):
for i, analysis in runs_analysis:
locals()[f"run_analysis_{i}"] = analysis or run_all_analysis
if run_analysis_1:
# some function that saves analysis 1 to local folder
if run_analysis_2:
# some function that saves analysis 2 to local folder
也就是说,当地人的文档明确表示不要使用它来修改变量:“注意:不应修改此字典的内容;更改可能不会影响解释器使用的局部变量和自由变量的值。
问题是,如果你有一些IDE,Intellisense会告诉你变量没有声明,所以我会选择一些更具可读性的东西,可能使用kwargs:
def save_locally(self,
run_all_analysis: bool = False,
**kwargs):
if run_all_analysis:
for x in range(20):
kwargs[f"run_analysis_{x}"] = True
if kwargs["run_analysis_1"]:
# some function that saves analysis 1 to local folder
if kwargs["run_analysis_2"]:
# some function that saves analysis 2 to local folder
评论
通过利用 python 类的属性来做到这一点的一种非常卡顿的方法:__dict__
class TestClass:
NUM_ANALYSIS = 20
def save_locally(
self,
run_all_analysis: bool = False,
run_analysis_1: bool = False,
run_analysis_2: bool = False,
):
self.run_all_analysis = run_all_analysis
if run_all_analysis:
for index in range(1, self.NUM_ANALYSIS + 1):
self.__dict__[f"run_analysis_{index}"] = True
print(self.run_analysis_1) # prints: True
print(self.run_analysis_2) # prints: True
test_class = TestClass()
test_class.save_locally(run_all_analysis=True)
如果您使用 kwargs 字典而不是逐个定义每个参数,则要简单得多:run_analysis_<n>
def save_locally(self,
run_all_analysis: bool = False,
**kwargs):
if run_all_analysis:
for key in kwargs.keys():
kwargs[key] = True
if kwargs["run_analysis_1"] is True:
# some function that saves analysis 1 to local folder
if kwargs["run_analysis_2"] is True:
# some function that saves analysis 2 to local folder
如果我们想让事情变得更紧凑,无论您必须执行多少分析,都只有一个 if,我们将创建一个列表,其中包含与每个分析标志关联的函数:
def save_locally(self,
run_all_analysis: bool = False,
**kwargs):
analysis_functions = [f1, f2, f3]
for n, function in enumerate(analysis_functions, start=1):
if run_all_analysis or kwargs.get(f"run_analysis_{n}", False) is True:
function() # Some function that saves analysis n to local folder.
与其找到一些方法来更改所有变量,不如使用:run_analysis_x
or
def save_locally(self,
run_all_analysis: bool = False,
run_analysis_1: bool = False,
run_analysis_2: bool = False):
if run_analysis_1 or run_all_analysis:
# some function that saves analysis 1 to local folder
if run_analysis_2 or run_all_analysis:
# some function that saves analysis 2 to local folder
更好的是,将函数及其相应的布尔值分组到列表中,然后循环:
def save_locally(self, run_all_analysis: bool = False, *run_bools):
analysis_funcs = [do_analysis_1, do_analysis_2, ...]
for run_bool, func in zip(run_bools, analysis_funcs):
if run_bool or run_all_analysis:
func()
...
评论
if run_analysis_1 or run_all_analysis: