What does ,= mean in python?

It’s a form of tuple unpacking. With parentheses:

(plot1,) = ax01.plot(t,yp1,'b-')

ax01.plot() returns a tuple containing one element, and this element is assigned to plot1. Without that comma (and possibly the parentheses), plot1 would have been assigned the whole tuple. Observe the difference between a and b in the following example:

>>> def foo():
...     return (1,)
... 
>>> (a,) = foo()
>>> b = foo()
>>> a
1
>>> b
(1,)

You can omit the parentheses both in (a,) and (1,), I left them for the sake of clarity.

Leave a Comment