Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ,= mean in python?

I wonder what ,= or , = means in python?

Example from matplotlib:

plot1, = ax01.plot(t,yp1,'b-') 
like image 357
benni Avatar asked Jun 02 '15 16:06

benni


1 Answers

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.

like image 51
Stefano Sanfilippo Avatar answered Sep 28 '22 04:09

Stefano Sanfilippo