12

I'm trying to make an animated plot. Here is an example code:

from pylab import *
import time

ion()

tstart = time.time()               # for profiling
x = arange(0,2*pi,0.01)            # x-array
line, = plot(x,sin(x))
for i in arange(1,200):
    line.set_ydata(sin(x+i/10.0))  # update the data
    draw()                         # redraw the canvas

print 'FPS:' , 200/(time.time()-tstart)

I don't understand the line,. Without comma, the code doesn't work.

enedene
  • 3,525
  • 6
  • 34
  • 41
  • 3
    possible duplicate of [An unusual Python syntax element frequently used in Matplotlib](http://stackoverflow.com/questions/9731779/an-unusual-python-syntax-element-frequently-used-in-matplotlib) – Ignacio Vazquez-Abrams May 02 '12 at 22:27

2 Answers2

17

The comma is Python syntax that denotes either a single-element tuple. E.g.,

>>> tuple([1])
(1,)

In this case, it is used for argument unpacking: plot returns a single-element list, which is unpacked into line:

>>> x, y = [1, 2]
>>> x
1
>>> y
2
>>> z, = [3]
>>> z
3

An alternative, perhaps more readable way of doing this is to use list-like syntax:

>>> [z] = [4]
>>> z
4

though the z, = is more common in Python code.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
0

case1:

a=1,
type(a)
tuple

case2:

a=1
type(a)
int
Minux
  • 51
  • 1
  • 8