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


Groups > comp.lang.python > #85216

Re: meaning of: line, =

Date 2015-02-04 14:38 +0000
From Albert-Jan Roskam <fomcl@yahoo.com>
References <CAPTjJmpqx1Cmm89Lg6-XL4-xdM+xD6=GHKvWb2zYTe4=11ZPPg@mail.gmail.com>
Subject Re: meaning of: line, =
Newsgroups comp.lang.python
Message-ID <mailman.18464.1423061056.18130.python-list@python.org> (permalink)

Show all headers | View raw



----- Original Message -----

> From: Chris Angelico <rosuav@gmail.com>
> To: 
> Cc: "python-list@python.org" <python-list@python.org>
> Sent: Wednesday, February 4, 2015 3:24 PM
> Subject: Re: meaning of: line, =
> 
> 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.



I have also never seen this before, but perhaps this:

>>> f = lambda: [42]
>>> result, = f()
>>> result
42

... is slightly cleaner than this:
>>> result = f()[0]
>>> result
42

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


Thread

Re: meaning of: line, = Albert-Jan Roskam <fomcl@yahoo.com> - 2015-02-04 14:38 +0000
  Re: meaning of: line, = Rustom Mody <rustompmody@gmail.com> - 2015-02-04 07:09 -0800
    Re: meaning of: line, = Peter Otten <__peter__@web.de> - 2015-02-04 18:36 +0100
    Re: meaning of: line, = Chris Angelico <rosuav@gmail.com> - 2015-02-05 08:18 +1100
    Re: meaning of: line, = Devin Jeanpierre <jeanpierreda@gmail.com> - 2015-02-05 01:10 -0800
      Re: meaning of: line, = Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-02-05 20:40 +1100
        Re: meaning of: line, = Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-05 09:08 -0700
          Re: meaning of: line, = Rustom Mody <rustompmody@gmail.com> - 2015-02-05 08:45 -0800
            Re: meaning of: line, = Skip Montanaro <skip.montanaro@gmail.com> - 2015-02-05 10:56 -0600
            Re: meaning of: line, = Tim Chase <python.list@tim.thechases.com> - 2015-02-05 11:00 -0600
            Re: meaning of: line, = Rustom Mody <rustompmody@gmail.com> - 2015-02-05 08:59 -0800
              Re: meaning of: line, = Devin Jeanpierre <jeanpierreda@gmail.com> - 2015-02-06 05:09 -0800
                Re: meaning of: line, = Rustom Mody <rustompmody@gmail.com> - 2015-02-06 05:20 -0800
                Re: meaning of: line, = Rustom Mody <rustompmody@gmail.com> - 2015-02-06 06:03 -0800
        Re: meaning of: line, = Tim Chase <python.list@tim.thechases.com> - 2015-02-05 10:21 -0600
        Re: meaning of: line, = Devin Jeanpierre <jeanpierreda@gmail.com> - 2015-02-05 17:12 -0800
        Re: meaning of: line, = Chris Angelico <rosuav@gmail.com> - 2015-02-06 12:17 +1100
          Re: meaning of: line, = Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-02-06 17:52 +1300
      Re: meaning of: line, = Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-02-06 12:30 +1300

csiph-web