|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
求助各位大神,为什么上边的代码运行得了,而下边的代码运行不了,为什么一定要给x,y,z赋给另一个变量才可以?
>>> x,y,z = 1,2,3
>>> h = x,y,z
>>> type(h)
<class 'tuple'>
>>> x,y,z = 1,2,3
>>> type(x,y,z)
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
type(x,y,z)
TypeError: type.__new__() argument 1 must be str, not int
- >>> x, y, z = 1, 2, 3
- >>> #此时 x 的值是 1,y 的值是 2,z 的值是 3
- >>> h = x, y, z
- >>> h
- (1, 2, 3)
- >>> #此时 h 是一个元组,值是 (1, 2, 3)
- >>> #再执行 type(h) 相当于在执行 type((1, 2, 3))
- >>> type(h)
- <class 'tuple'>
- >>> #而在执行 type(x, y, z) 的时候, python 会认为你传入了 3 个参数
- >>> #此时就会报错
- >>> type(x, y, z)
- Traceback (most recent call last):
- File "<pyshell#9>", line 1, in <module>
- type(x, y, z)
- TypeError: type.__new__() argument 1 must be str, not int
- >>> #想要不通过 h 变量就实现 type(h) 一样的效果,可以这样写:
- >>> type((x, y, z))
- <class 'tuple'>
- >>>
复制代码
|
|