|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
>>> def test(**params):
print(params)
>>> a = {1:2, 3:4}
>>> a
{1: 2, 3: 4}
>>> type(a)
<class 'dict'>
>>> test(**a)
Traceback (most recent call last):
File "<pyshell#46>", line 1, in <module>
test(**a)
TypeError: keywords must be strings
1.为什么这里输入函数的参数中的key一定要str类型呀?
>>> test(a)
Traceback (most recent call last):
File "<pyshell#39>", line 1, in <module>
test(a)
TypeError: test() takes 0 positional arguments but 1 was given
>>> test(1,2,3,4)
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
test(1,2,3,4)
TypeError: test() takes 0 positional arguments but 4 were given
2.为什么我用的是字典形式的收集参数,在报错时却提示我位置参数错误,在字典中应该也不存在位置这个说法吧
这样就可以了。
- >>> a
- {'x': 1, 'y': 2}
- >>> test(**a)
- {'x': 1, 'y': 2}
复制代码
**解包相当于x = 1, y = 2,这是符合语法的
但a = {1:2, 3:4},就变成 1= 2, 3 = 4就出错了
|
|