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

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

WBOY
Release: 2016-06-10 15:10:42
Original
1975 people have browsed it

本文实例讲述了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'>
Copy after login

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

>>> print c
[]
>>> print d
([],)
>>> print a
1
>>> print b
(2,)
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template