如何将参数传递给本身作为 kwarg 传递的函数
How to pass arguments to a function that itself is passed as a kwarg
在下面的示例中,我试图将参数传递给一个本身已作为 kwarg 传递的函数。在以下示例中,我未能成功将参数从 class 'TestClass' 中传递给函数 'func':
import sys, threading; from threading import Thread
def func(kwargs):
print('IN:', sys._getframe(0).f_code.co_name)
for key, value in kwargs.items() :
print ('KEY:', key, ', VAL:', value, sep='')
class TestClass(Thread):
def __init__(self, name = sys._getframe(0).f_code.co_name, kwargs = None):
Thread.__init__(self)
self.name = name
self.kwargs = kwargs
print('IN:', self.name)
def run(self):
func = self.kwargs['func']
func_kwargs_inner = {'arg_1': 'INNER-1', 'arg_2': 'INNER-2'}
func() # how to pass func_kwargs_inner to func?
def main():
func_kwargs = {'arg_1': 'OUTER-1', 'arg_2': 'OUTER-2'} # these get passed
# func_kwargs = {} # func_kwargs never gets populated
kwargs = {'func': (lambda: func(func_kwargs))}
test = TestClass(name='my-test', kwargs=kwargs)
test.start()
print('PROGRAM END')
if __name__ == '__main__':
main()
如果我尝试将 'func_kwargs_inner' 传递给 'func()',我会得到语法错误;如果我将参数列表留空 - 如示例所示 - 结果为:
IN: my-test
IN: func
KEY:arg_1, VAL:OUTER-1
KEY:arg_2, VAL:OUTER-2
PROGRAM END
而一旦我找到正确传递参数的方法所需的输出是:
IN: my-test
IN: func
KEY:arg_1, VAL:INNER-1
KEY:arg_2, VAL:INNER-2
PROGRAM END
如何将 'func_kwargs_inner' 传递给 'func()'?
似乎如果你做显而易见的事情,那么它就会起作用,并且你的代码目前明确地避免传递你想要的参数。具体来说,在您的 TestClass.run
中,您没有将任何参数传递给 func
,而是依赖于 hard-coded 到 lambda 表达式中的函数参数。所以改变你的行:
func() # how to pass func_kwargs_inner to func?
传递参数:
func(func_kwargs_inner)
然后在 main
中,而不是那个 lambda
表达式:
kwargs = {'func': (lambda: func(func_kwargs))}
简单传递函数对象本身:
kwargs = {'func': func}
然后你得到预期的输出:
IN: my-test
IN: func
PROGRAM END
KEY:arg_1, VAL:INNER-1
KEY:arg_2, VAL:INNER-2
在下面的示例中,我试图将参数传递给一个本身已作为 kwarg 传递的函数。在以下示例中,我未能成功将参数从 class 'TestClass' 中传递给函数 'func':
import sys, threading; from threading import Thread
def func(kwargs):
print('IN:', sys._getframe(0).f_code.co_name)
for key, value in kwargs.items() :
print ('KEY:', key, ', VAL:', value, sep='')
class TestClass(Thread):
def __init__(self, name = sys._getframe(0).f_code.co_name, kwargs = None):
Thread.__init__(self)
self.name = name
self.kwargs = kwargs
print('IN:', self.name)
def run(self):
func = self.kwargs['func']
func_kwargs_inner = {'arg_1': 'INNER-1', 'arg_2': 'INNER-2'}
func() # how to pass func_kwargs_inner to func?
def main():
func_kwargs = {'arg_1': 'OUTER-1', 'arg_2': 'OUTER-2'} # these get passed
# func_kwargs = {} # func_kwargs never gets populated
kwargs = {'func': (lambda: func(func_kwargs))}
test = TestClass(name='my-test', kwargs=kwargs)
test.start()
print('PROGRAM END')
if __name__ == '__main__':
main()
如果我尝试将 'func_kwargs_inner' 传递给 'func()',我会得到语法错误;如果我将参数列表留空 - 如示例所示 - 结果为:
IN: my-test
IN: func
KEY:arg_1, VAL:OUTER-1
KEY:arg_2, VAL:OUTER-2
PROGRAM END
而一旦我找到正确传递参数的方法所需的输出是:
IN: my-test
IN: func
KEY:arg_1, VAL:INNER-1
KEY:arg_2, VAL:INNER-2
PROGRAM END
如何将 'func_kwargs_inner' 传递给 'func()'?
似乎如果你做显而易见的事情,那么它就会起作用,并且你的代码目前明确地避免传递你想要的参数。具体来说,在您的 TestClass.run
中,您没有将任何参数传递给 func
,而是依赖于 hard-coded 到 lambda 表达式中的函数参数。所以改变你的行:
func() # how to pass func_kwargs_inner to func?
传递参数:
func(func_kwargs_inner)
然后在 main
中,而不是那个 lambda
表达式:
kwargs = {'func': (lambda: func(func_kwargs))}
简单传递函数对象本身:
kwargs = {'func': func}
然后你得到预期的输出:
IN: my-test
IN: func
PROGRAM END
KEY:arg_1, VAL:INNER-1
KEY:arg_2, VAL:INNER-2