【发布时间】:2017-12-26 23:24:21
【问题描述】:
在 animate_decay.py 的 matplotlib 示例中,return 语句使用尾随逗号,如:
return line,
并且该函数是普通函数,不是生成器函数。
所以,我编写了同一个函数的两个版本,一个带有尾随逗号,另一个没有:
def no_trailing_comma(x):
return x + [10]
def trailing_comma(x):
return x + [10],
data = [1, 2, 3]
print("With trailing comma", trailing_comma(data))
print("With no trailing comma", no_trailing_comma(data))
无论哪种情况,输出都是相同的:
尾随逗号 [1, 2, 3, 10]
没有尾随逗号 [1, 2, 3, 10]
语言规范 (Python 3.6) 没有特别提到 return 语句中的尾随逗号。我错过了什么吗?
【问题讨论】:
-
@user2357112 奇怪。您使用的是 Python 3.5,而我使用的是 3.6。我以我的输出发誓(你会的,以你的)。
-
,在这种情况下是tuple文字,特别是具有单个值的元组(我正在运行 3.6 和类似 2.6、2.7、3.5 等。它导致([1, 2, 3, 10],)- 一个具有一个值的元组。 -
在 3.6 上尝试仍然会产生一个带有逗号结尾的元组。
标签: python matplotlib return