可选择将额外的字典传递给方法/函数
Optionally passing an extra dictionary to a method / function
我正在编写如下函数:
def create_objection(name, position, **kwargs):
dicct = {'name':name,
'position':position}
print(len(kwargs))
dicct.update({'extra_info': kwargs})
return dicct
此函数应该始终获取名称和位置,并最终获取 for 或字典中 key/value 对的集合。
我想要的是以下工作方式:
create_objection('c1',{'t':1})
输出:
0
{'name': 'c1', 'position': {'t': 1}, 'extra_info': {}}
当我尝试这个时,出现错误:
create_objection('c1',{'t':1},{'whatever':[3453,3453]},{'whatever2':[34,34]})
错误:
TypeError Traceback (most recent call last)
/tmp/ipykernel_32291/2973311999.py in <module>
----> 1 create_objection('c1',{'t':1},{'tt':2,'ttt':'3'})
TypeError: create_objection() takes 2 positional arguments but 3 were given
我想得到:
{'name': 'c1', 'position': {'t': 1}, 'extra_info': [{'whatever':[3453,3453]},{'whatever2':[34,34]}]}
如何进行?
尝试:
def create_objection(*args, **kwargs):
dicct = {'name': args[0], 'position': args[1]}
if not kwargs:
print(len(kwargs))
dicct.update{{'extra_info': kwargs})
return dicct
忽略上面的,把它留给下一批,但OP必须改变方法看起来像:
def create_objection(name=None, position=None, **kwargs):
dicct = {'name': name, 'position': position}
if not kwargs:
print(len(kwargs))
dicct.update{{'extra_info': kwargs})
return dicct
Here is a similar issue
看起来你需要在调用你的函数时解压你的字典。
create_objection('c1',{'t':1},**{'whatever':[3453,3453]},{'whatever2':[34,34]})
注意 **
这里发生的事情是你定义了一个只有两个位置参数的函数,而你正试图给它 3,正如错误消息告诉你的那样。
如果我们查看您的代码,那么您似乎也没有尝试使用关键字参数,因此没有使用 **kwargs
参数。
只需对您的代码稍作改动,我们就可以获得您想要的结果:
def create_objection(name, position, *args):
dicct = {'name': name,
'position': position}
if args:
dicct["extra_info"] = args
return dicct
x = create_objection('c1', {'t': 1})
z = create_objection('c1', {'t': 1}, {'whatever': [3453, 3453]}, {'whatever2': [34, 34]})
print(x)
print(z)
输出:
{'name': 'c1', 'position': {'t': 1}}
{'name': 'c1', 'position': {'t': 1}, 'extra_info': ({'whatever': [3453, 3453]}, {'whatever2': [34, 34]})}
如果您还想包含其他关键字参数,那么您可以像这样对该变量进行另一个循环:
def create_objection(name, position, *args, **kwargs):
dicct = {'name': name,
'position': position}
if args:
dicct["extra_info"] = args
for key, value in kwargs.items():
dicct[key] = value
return dicct
y = create_objection('c1', {'t': 1}, {'whatever': [3453, 3453]}, thisisakeyword="totally a keyword!")
print(y)
输出:
{'name': 'c1', 'position': {'t': 1}, 'extra_info': ({'whatever': [3453, 3453]},), 'thisisakeyword': 'totally a keyword!'}
我正在编写如下函数:
def create_objection(name, position, **kwargs):
dicct = {'name':name,
'position':position}
print(len(kwargs))
dicct.update({'extra_info': kwargs})
return dicct
此函数应该始终获取名称和位置,并最终获取 for 或字典中 key/value 对的集合。 我想要的是以下工作方式:
create_objection('c1',{'t':1})
输出:
0
{'name': 'c1', 'position': {'t': 1}, 'extra_info': {}}
当我尝试这个时,出现错误:
create_objection('c1',{'t':1},{'whatever':[3453,3453]},{'whatever2':[34,34]})
错误:
TypeError Traceback (most recent call last)
/tmp/ipykernel_32291/2973311999.py in <module>
----> 1 create_objection('c1',{'t':1},{'tt':2,'ttt':'3'})
TypeError: create_objection() takes 2 positional arguments but 3 were given
我想得到:
{'name': 'c1', 'position': {'t': 1}, 'extra_info': [{'whatever':[3453,3453]},{'whatever2':[34,34]}]}
如何进行?
尝试:
def create_objection(*args, **kwargs):
dicct = {'name': args[0], 'position': args[1]}
if not kwargs:
print(len(kwargs))
dicct.update{{'extra_info': kwargs})
return dicct
忽略上面的,把它留给下一批,但OP必须改变方法看起来像:
def create_objection(name=None, position=None, **kwargs):
dicct = {'name': name, 'position': position}
if not kwargs:
print(len(kwargs))
dicct.update{{'extra_info': kwargs})
return dicct
Here is a similar issue
看起来你需要在调用你的函数时解压你的字典。
create_objection('c1',{'t':1},**{'whatever':[3453,3453]},{'whatever2':[34,34]})
注意 **
这里发生的事情是你定义了一个只有两个位置参数的函数,而你正试图给它 3,正如错误消息告诉你的那样。
如果我们查看您的代码,那么您似乎也没有尝试使用关键字参数,因此没有使用 **kwargs
参数。
只需对您的代码稍作改动,我们就可以获得您想要的结果:
def create_objection(name, position, *args):
dicct = {'name': name,
'position': position}
if args:
dicct["extra_info"] = args
return dicct
x = create_objection('c1', {'t': 1})
z = create_objection('c1', {'t': 1}, {'whatever': [3453, 3453]}, {'whatever2': [34, 34]})
print(x)
print(z)
输出:
{'name': 'c1', 'position': {'t': 1}}
{'name': 'c1', 'position': {'t': 1}, 'extra_info': ({'whatever': [3453, 3453]}, {'whatever2': [34, 34]})}
如果您还想包含其他关键字参数,那么您可以像这样对该变量进行另一个循环:
def create_objection(name, position, *args, **kwargs):
dicct = {'name': name,
'position': position}
if args:
dicct["extra_info"] = args
for key, value in kwargs.items():
dicct[key] = value
return dicct
y = create_objection('c1', {'t': 1}, {'whatever': [3453, 3453]}, thisisakeyword="totally a keyword!")
print(y)
输出:
{'name': 'c1', 'position': {'t': 1}, 'extra_info': ({'whatever': [3453, 3453]},), 'thisisakeyword': 'totally a keyword!'}