注意Python赋值表达式后的逗号

来源:互联网 发布:知乎文章怎么看 编辑:程序博客网 时间:2024/06/09 19:09

IDLE 2.6.2

>>> a = 1

>>> b = 2,

 

>>> print type(a)
<type 'int'>

 

>>> print type(b)
<type 'tuple'>

 

>>> c = []
>>> d = [],

 

>>> print type(c)
<type 'list'>

 

>>> print type(d)
<type 'tuple'>

 

赋值表达式的后面加了逗号后,会自动得到一个tuple的对象,在作一些与类型相关的工作或需要序列化时,是不能得到期望的结果的。工作中碰到类似灵异现象时,可以把这个放到自己的checklist中了。


>>> print c
[]

 

>>> print d
([],)

 

>>> print a
1

 

>>> print b
(2,)

原创粉丝点击