[Tutor] comma in an assignment

Alan Gauld alan.gauld at btinternet.com
Wed Oct 23 01:51:17 CEST 2013


On 22/10/13 20:20, Key, Gregory E (E S SF RNA FSF 1 C) wrote:
> I understand that a comma in Python is a separator and not an operator.
> In some of the MatPlotLib examples I see code like this:
> line1, = ax1.plot(t, y1, lw=2, color='red', label='1 HZ')
> What does the comma do in an assignment statement?

Caveat: I know nothing about MatPlotLib...

But a comma after a name like that usually means its part of a
tuple (a single element tuple) so you could rewrite it like:

(line1,) = ax1.plot(t, y1, lw=2, color='red', label='1 HZ')

But I've no idea why you'd want to do that or why MatPlotLib
apparently does. Maybe someone who uses MatPlotLib will reveal
all...

A wee bit of experimenting suggests ou can use it to unpack
a single valued tuple. So presumably ax1.plot() returns a
single value tuple. Without the comma line1 would also be
a tuple but with the comma it takes on the value inside
instead...

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list