【发布时间】:2013-04-12 16:33:49
【问题描述】:
我想知道逗号在这段代码中的作用:
line, =
以下示例显示了该行为:
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, = ax.plot([], [], lw=2)
print("First:")
print(line)
print("Second:")
line = ax.plot([], [], lw=2)
print(line)
结果:
First:
Line2D(_line0)
Second:
[<matplotlib.lines.Line2D object at 0xb137c4c>]
当我尝试使用 line 变量时真的变得很混乱,我不知道是否使用尾随逗号:
def init():
line.set_data([], [])
return line,
或者有没有更好的方法来避免逗号?
【问题讨论】:
-
这在你有一个包含单个项目的容器时很有用,而不是
x = L[0],你可以只做x, = L
标签: python