简体   繁体   English

使用kwargs将参数传递给函数-Python

[英]passing arguments to a function using kwargs - Python

I have the following code snippet: 我有以下代码片段:

kwargs = {'inventory_file' : self.inventory_file}
for ts in self.test_sets_list:
   current_ts = TestSet(name=ts,
                        test_cases=self.test_sets_list[ts],
                        om_ip_address=self.om_ip_address,
                        **kwargs)


class TestSet(object):
   def __init__(self, name, test_cases, om_ip_address, **kwargs):


class TestCase(object):
        def __init__(self, parameters, config_file, test_failure,
                 om_ip_address, **kwargs):

I know I'm probably doing wrong, because in **kwargs I'm unpacking the values in kwargs . 我知道我可能做错了,因为在**kwargs我在拆箱值kwargs What I want is to pass the arguments of kwargs all the way to TestCase . 我想要的是将kwargs的参数一直传递给TestCase Inside TestSet I'm trying to pass the kwargs argument: TestSet内部,我试图传递kwargs参数:

instance = TestCase(parameters=params, 
                    config_file=cnf,
                    om_ip_address=om_ip_address,
                    **kwargs)

However I'm getting the following error: 但是我收到以下错误:

Traceback (most recent call last):
  File "main.py", line 258, in <module>
    main(args)
  File "main.py", line 217, in main
    operation.collect_tests()
  File "main.py", line 157, in collect_tests
    **kwargs)
  File "test_set.py", line 47, in __init__
    self.__instantiate_module(tc, om_ip_address)
  File "test_set.py", line 78, in __instantiate_module
    **self.kwargs)
TypeError: __init__() got an unexpected keyword argument 'inventory_file

The class is instantiated via: 该类通过以下方式实例化:

    my_class = getattr(module, test_cases['name'])
    instance = my_class(parameters=params, 
                        config_file=cnf,
                        om_ip_address=om_ip_address)

How can I be able to pass kwargs all the way to TestCase ? 如何将kwargs一直传递给TestCase

Calling a function with **kwargs , where kwargs = {'inventory_file' : self.inventory_file} is the same as calling **kwargs调用函数,其中kwargs = {'inventory_file' : self.inventory_file}与调用相同

function(inventory_file=self.inventory_file)

and if the definition of function doesn't have inventory_file in the accepted params list, it will throw the error you got there. 并且如果function的定义在接受的参数列表中没有清单文件,它将抛出您到达的错误。 Two ways to solve this: 解决此问题的两种方法:

class TestCase(object):
    # add inventory_file to the params list with a default value
    def __init__(self, parameters, config_file, test_failure,
             om_ip_address, inventory_file=None, **kwargs):
        self.inventory_file = inventory_file

or: 要么:

class TestCase(object):
    # check for the inventory file in the kwargs dict
    def __init__(self, parameters, config_file, test_failure,
             om_ip_address, **kwargs):
        if 'inventory_file' in kwargs:
             self.inventory_file = kwargs['inventory_file']

and in both cases: 在两种情况下:

>>> t = TestCase('param1','param2','param3','param4', **{'inventory_file':'file'})
>>> t.inventory_file
'file'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM