Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #85214

Re: meaning of: line, =

References <54d227ef$0$3292$426a74cc@news.free.fr>
Date 2015-02-05 01:24 +1100
Subject Re: meaning of: line, =
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.18462.1423059895.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Feb 5, 2015 at 1:08 AM, ast <nomail@invalid.com> wrote:
> I dont understand why there is a comma just after line in the following
> command:
>
> line, = plt.plot(x, np.sin(x), '--', linewidth=2)
>
>
> I never saw that before
>
> Found here:
> http://matplotlib.org/examples/lines_bars_and_markers/line_demo_dash_control.html
>

That's a slightly unusual form of unpacking. Compare:

def get_values():
    return 5, 7, 2

x, y, z = get_values()

This is like "x = 5; y = 7; z = 2", because it unpacks the function's
return value into those three targets.

What you have is exactly the same, except that it has only one target.
So it's expecting plt.plot() to return an iterable with exactly one
thing in it, and it'll unpack it and put that thing into line:

def get_packaged_value():
    return [42]

x, = get_packaged_value()

This is equivalent to "x = 42". I don't know matplotlib, so I don't
know what it's returning or why, but as long as it's iterable and
yields exactly one thing, this will work.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

meaning of:  line, = "ast" <nomail@invalid.com> - 2015-02-04 15:08 +0100
  Re: meaning of: line, = leo kirotawa <kirotawa@gmail.com> - 2015-02-04 12:22 -0200
  Re: meaning of: line, = Chris Angelico <rosuav@gmail.com> - 2015-02-05 01:24 +1100
  Re: meaning of:  line, = "ast" <nomail@invalid.com> - 2015-02-05 09:28 +0100

csiph-web