2014-02-22 43 views
2

a recent question of mine中,我引用了Jake Vanderplas的一些代碼。人們可以找到下面的代碼:`return line,```return line`之間的區別

from matplotlib import pyplot as plt 
from matplotlib import animation 

fig = plt.figure() 

ax = plt.axes(xlim=(0, 2), ylim=(0, 100)) 

line, = plt.plot([], []) 

def init(): 
    line.set_data([], []) 
    return line, 

def animate(i): 
    line.set_data([0, 2], [0,i]) 
    return line, 

anim = animation.FuncAnimation(fig, animate, init_func=init, 
           frames=100, interval=20, blit=True) 

plt.show() 

initanimate功能,迴歸「價值」是line,(用逗號分隔)。

問題:與返回「價值」是否會有差異,將是line(白色逗號)?

由於

回答

6

line,是在它一個對象的元組。 line只是線條對象。

In [80]: line = object() 

In [81]: line, 
Out[81]: (<object at 0x9ee7fa8>,) 
+0

...很多人會寫'(line,)'來生成那個元組,甚至在括號是多餘的上下文中。忽略沒有它們的逗號很容易。雖然我想如果我們沒有通過提供parens互相幫助,那麼我們會更好地發現逗號:-) –

+0

謝謝。在具有一個元素的元組的上下文中有什麼優勢? – cjorssen

+0

@cjorssen:'init'和'animate'在這裏返回元組以符合'FuncAnimation'的規範。 [文檔說](http://matplotlib.org/api/animation_api.html#matplotlib.animation.FuncAnimation),「如果blit = True,func和init_func應該返回一個**迭代** drawables來清除。」 – unutbu

相關問題