[英]Passing method with args to another function with kwargs
I have two functions that differ by one argument that I would like to pass in accordingly based on a given case... 我有两个函数,它们的一个参数不同,我希望根据给定的情况将其传递给我。
def func1(a, b, c):
print a, b, c
def func2(a, b, c, d=False)
print a, b, c, d
def run(func, **kwargs):
if b is None:
b = 999
func(**kwargs)
run(func1, a=1, b=None, c=3)
I am unable to get this to work as it complains that b
is being referenced before assignment. 我无法使它正常工作,因为它抱怨分配前已引用了
b
。
def func1(a, b, c):
print a, b, c
def func2(a, b, c, d=False):
print a, b, c, d
def run(func, **kwargs):
if 'b' in kwargs:
if kwargs['b'] is None:
kwargs['b'] = 999
func(**kwargs)
run(func1, a=1, b=None, c=3)
Access b by: 通过以下方式访问b:
if kwargs['b'] is None:
So, The run function would be: 因此,运行功能将是:
def run(func, **kwargs):
kwargs['b'] = kwargs.get('b', 999)
func(**kwargs)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.