[英]Calling function from a list using named arguments
Lets say I have a list of function calls and their respective arguments, something like the following假设我有一个 function 调用及其各自的 arguments 的列表,类似于以下内容
def printStuff(val1, val2, val3, printGotHere=False, saveValsToFile=False):
allVals = [val1, val2, val3]
if printGotHere:
print('Got Here!')
for val in allVals:
print(val)
if saveValsToFile:
with open('vals.text', 'w') as fp:
for line in allVals:
fp.write("%s\n" % str(line))
funcList = []
funcList.append({'func': printStuff, 'args': [1, 2, 3]})
funcList.append({'func': printStuff, 'args': [4, 5, 6]})
funcList.append({'func': printStuff, 'args': [7, 8, 9]})
for funcAndArgs in funcList:
funcAndArgs['func'](*funcAndArgs['args'])
That works fine, but lets say I want to specify one of the named arguments like "saveValsToFile", without putting any of the other optional variables.这很好用,但是假设我想指定一个名为 arguments 的名称,例如“saveValsToFile”,而不放置任何其他可选变量。 So a normal frunction call for that would look like
printStuff(1, 2, 3, saveValsToFile=True)
.所以一个正常的函数调用看起来像
printStuff(1, 2, 3, saveValsToFile=True)
。 If I try to put that named variable into a list like the method above, it gives and error... but I'm not sure how to format so it knows to treat it as a named variable.如果我尝试将该命名变量放入上述方法的列表中,它会给出错误...但我不确定如何格式化,因此它知道将其视为命名变量。 So I want to do something like this
所以我想做这样的事情
funcList.append({'func': printStuff, 'args': [1, 2, 3, saveValsToFile=True]})
I'm actually calling all of these functions in the lists from through lambda so I can add a thread to a queue, and so I had to make a helper function so can pass more than one argument to the function.我实际上是从 lambda 调用列表中的所有这些函数,这样我就可以向队列中添加一个线程,所以我必须创建一个助手 function 以便可以将多个参数传递给 ZC1C425268E683894F14AB57A。 Not sure if that changes things, but this is how i have that part setup.
不确定这是否会改变事情,但这就是我设置该部分的方式。
def customFunctionCall(funcArgDict):
return funcArgDict['func'](*funcArgDict['args'])
threads = []
que = Queue()
for funcAndArgs in funcList:
t = threading.Thread(target=lambda q, arg1: q.put(customFunctionCall(arg1)), args=(que, funcAndArgs))
t.start()
threads.append(t)
for t in threads:
t.join()
while not que.empty():
result = que.get()
print(result)
You need to add a new kwargs
parameter that is a dictionary of keyword arguments:您需要添加一个新的
kwargs
参数,该参数是关键字 arguments 的字典:
def printStuff(val1, val2, val3, printGotHere=False, saveValsToFile=False):
allVals = [val1, val2, val3]
if printGotHere:
print('Got Here!')
for val in allVals:
print(val)
if saveValsToFile:
with open('vals.text', 'w') as fp:
for line in allVals:
fp.write("%s\n" % str(line))
funcList = []
funcList.append({'func': printStuff, 'args': [1, 2, 3]})
funcList.append({'func': printStuff, 'args': [4, 5, 6]})
funcList.append({'func': printStuff, 'args': [7, 8, 9], 'kwargs': {'printGotHere': True}})
for funcAndArgs in funcList:
funcAndArgs['func'](*funcAndArgs['args'], **funcAndArgs.get('kwargs', {}))
Running the above results in:运行上述结果:
1
2
3
4
5
6
Got Here!
7
8
9
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.