以下是我的功能
def list_abc(self, name, id, keywords):
cmd = ABC() //ABC is a class
cmd.id=id
cmd.name=name
cmd.keywords=keywords
return ABC(cmd)
我希望在Python中使用** kwargs传递name
,id
和'keywords
。
知道怎么做吗?
提前致谢。
答案 0 :(得分:6)
kwargs = {'name': 'Frank', 'id': 999, 'keywords': ['cool', 'smart']}
result = self.list_abc(**kwargs)
答案 1 :(得分:3)
def list_abc(self, **kwargs):
# check if 'name' in kwargs, etc
cmd = ABC()
cmd.id = kwargs['id']
cmd.name = kwargs['name']
cmd.keywords = kwargs['keywords']
return ABC(cmd) # not sure what ABC(ABC) does
...
some_instance.list_abc(name='name', id=1, keywords=['good', 'luck'])
# or, if have a dictionary containing name, id and keywords, you can pass it to the
# method as follows
data = {'name':'name', 'id':1, 'keywords':('a', 'b')}
some_instance.list_abc(**data)