
Hello all, Here is a bit of code that I am getting my head around. It's out of a book that I am using to teach myself matplotlib, a python library for making graphs etc. The following code is using an object-orientated approach in preparation for embedding the graph within an GUI application. <code> #!/usr/bin/env python # -*- coding: utf-8 -*- import matplotlib.pyplot as plt import numpy as np x = np.arange(0, 10, 0.1) y = np.random.randn(len(x)) fig = plt.figure() ax = fig.add_subplot(111) l, = plt.plot(x, y) t = ax.set_title('random numbers') plt.show() </code> The line l, = plt.plot(x, y) has got me slighty confused because l = plt.plot(x, y) also works, without the trailing comma after the l I was wondering if any Python coders might comment to the comma's significance. I've scanned my four books on Python and can't find any reference. Cheers, Chris

Im not to sure if there is any significance to the comma with only one variable, it can be used with multiple variables to allow neat things like x, y = y, x which swaps the value of x and y (the comma seperating the variables). Posting code will genrelly work a lot nicer if you post to something like gist <http://gist.github.com/>, which perserves stuff like formatting, provides syntax hightlighting which for this piece of code wasnt too relevent.

More correctly http://gist.github.com/252080 2009/12/9 Chris O'Halloran <cmoman(a)gmail.com>:
Like this
git://gist.github.com/252080.git
cool, thanks.
Posting code will genrelly work a lot nicer if you post to something like gist, which perserves stuff like formatting, provides syntax hightlighting which for this piece of code wasnt too relevent.

Yes, I wondered typo but it gets quoted later on in the text verbatim so whether the typo was reproduced or the comma is intentional is unclear. I 2009/12/9 James Pluck <papabearnz(a)gmail.com>:
On 9/12/2009 10:54 a.m., Chris O'Halloran wrote
I was wondering if any Python coders might comment to the comma's significance. I've scanned my four books on Python and can't find any reference.
typo?
J
_______________________________________________ wlug mailing list | wlug(a)list.waikato.ac.nz Unsubscribe: http://list.waikato.ac.nz/mailman/listinfo/wlug

yep, it takes more sharing code (snippets in particular) less frustating On Wed, Dec 9, 2009 at 1:09 PM, Chris O'Halloran <cmoman(a)gmail.com> wrote:
Yes, I wondered typo but it gets quoted later on in the text verbatim so whether the typo was reproduced or the comma is intentional is unclear.
I 2009/12/9 James Pluck <papabearnz(a)gmail.com>:
On 9/12/2009 10:54 a.m., Chris O'Halloran wrote
I was wondering if any Python coders might comment to the comma's significance. I've scanned my four books on Python and can't find any reference.
typo?
J
_______________________________________________ wlug mailing list | wlug(a)list.waikato.ac.nz Unsubscribe: http://list.waikato.ac.nz/mailman/listinfo/wlug
_______________________________________________ wlug mailing list | wlug(a)list.waikato.ac.nz Unsubscribe: http://list.waikato.ac.nz/mailman/listinfo/wlug

The line l, = plt.plot(x, y)
has got me slighty confused because
l = plt.plot(x, y)
also works, without the trailing comma after the l
I was wondering if any Python coders might comment to the comma's significance. I've scanned my four books on Python and can't find any reference.
plt.plot() returns an iterable type, and l, = plt.plot(x,y) will store the first result of the iterable return value into l. It looks like it's returning a list that contains one value though, because if the list contains more than one value then l, = ..... would fail Not having the , works as well - l will then be a list with one value, rather than the first value of the list. def list1(): f = list() f.append(1) return f list1() returns a list with one entry, the integer 1:
list1() [1]
you can store this list in a variable foo:
foo = list1() foo [1]
Or you can store the first entry in the list in the variable foo by using foo, notation
foo, = list1() foo 1
There probably is reasonable difference between the variable l containing a list of whatever plot() returns, and between it having the first value in the list that plot() returns, however in your code snippet, l is not used again after being called, so in practice it doesn't make any difference.

Thanks for that useful reply. I'll tinker with the code some more later today. http://www.packtpub.com/matplotlib-python-development BTW, the more developed examples later in the book don't use the l, syntax Cheers, Chris 2009/12/9 Daniel Lawson <daniel(a)meta.net.nz>:
The line l, = plt.plot(x, y)
has got me slighty confused because
l = plt.plot(x, y)
also works, without the trailing comma after the l
I was wondering if any Python coders might comment to the comma's significance. I've scanned my four books on Python and can't find any reference.
plt.plot() returns an iterable type, and l, = plt.plot(x,y) will store the first result of the iterable return value into l.
It looks like it's returning a list that contains one value though, because if the list contains more than one value then l, = ..... would fail
Not having the , works as well - l will then be a list with one value, rather than the first value of the list.
def list1(): f = list() f.append(1) return f
list1() returns a list with one entry, the integer 1: >>> list1() [1]
you can store this list in a variable foo: >>> foo = list1() >>> foo [1]
Or you can store the first entry in the list in the variable foo by using foo, notation >>> foo, = list1() >>> foo 1
There probably is reasonable difference between the variable l containing a list of whatever plot() returns, and between it having the first value in the list that plot() returns, however in your code snippet, l is not used again after being called, so in practice it doesn't make any difference.
_______________________________________________ wlug mailing list | wlug(a)list.waikato.ac.nz Unsubscribe: http://list.waikato.ac.nz/mailman/listinfo/wlug

l, v = self.parse_file(self.mpllineEdit.text()) Later on the book uses the above syntax. I still need to do a little experimenting to fully appreciate this. Cheers, Chris 2009/12/9 Chris O'Halloran <cmoman(a)gmail.com>:
Thanks for that useful reply. I'll tinker with the code some more later today.
http://www.packtpub.com/matplotlib-python-development
BTW, the more developed examples later in the book don't use the l, syntax
Cheers,
Chris
2009/12/9 Daniel Lawson <daniel(a)meta.net.nz>:
The line l, = plt.plot(x, y)
has got me slighty confused because
l = plt.plot(x, y)
also works, without the trailing comma after the l
I was wondering if any Python coders might comment to the comma's significance. I've scanned my four books on Python and can't find any reference.
plt.plot() returns an iterable type, and l, = plt.plot(x,y) will store the first result of the iterable return value into l.
It looks like it's returning a list that contains one value though, because if the list contains more than one value then l, = ..... would fail
Not having the , works as well - l will then be a list with one value, rather than the first value of the list.
def list1(): f = list() f.append(1) return f
list1() returns a list with one entry, the integer 1: >>> list1() [1]
you can store this list in a variable foo: >>> foo = list1() >>> foo [1]
Or you can store the first entry in the list in the variable foo by using foo, notation >>> foo, = list1() >>> foo 1
There probably is reasonable difference between the variable l containing a list of whatever plot() returns, and between it having the first value in the list that plot() returns, however in your code snippet, l is not used again after being called, so in practice it doesn't make any difference.
_______________________________________________ wlug mailing list | wlug(a)list.waikato.ac.nz Unsubscribe: http://list.waikato.ac.nz/mailman/listinfo/wlug

l, v = self.parse_file(self.mpllineEdit.text())
Later on the book uses the above syntax.
I still need to do a little experimenting to fully appreciate this.
self.parse_file() returns a list (or a similar data construct) with two values
def return2(): ... return (1,2) ... a,b = return2() print a 1 print b 2
participants (4)
-
Chris O'Halloran
-
Daniel Lawson
-
James Pluck
-
Ronnie Collinson