Home  >  Article  >  Backend Development  >  Python赋值语句后逗号的作用分析

Python赋值语句后逗号的作用分析

WBOY
WBOYOriginal
2016-06-10 15:10:421929browse

本文实例讲述了Python赋值语句后逗号的作用。分享给大家供大家参考。具体分析如下:

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,)

希望本文所述对大家的Python程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn