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


Groups > comp.lang.python > #85217 > unrolled thread

Re: meaning of: line, =

Started byChris Angelico <rosuav@gmail.com>
First post2015-02-05 02:04 +1100
Last post2015-02-05 02:04 +1100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: meaning of: line, = Chris Angelico <rosuav@gmail.com> - 2015-02-05 02:04 +1100

#85217 — Re: meaning of: line, =

FromChris Angelico <rosuav@gmail.com>
Date2015-02-05 02:04 +1100
SubjectRe: meaning of: line, =
Message-ID<mailman.18465.1423062269.18130.python-list@python.org>
On Thu, Feb 5, 2015 at 1:38 AM, Albert-Jan Roskam <fomcl@yahoo.com> wrote:
> 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

They're not technically identical. If the thing returned is
subscriptable (as with your list example), then I would definitely
subscript it rather than unpacking; but if it's something iterable but
not subscriptable, the unpack will still work.

>>> def f(): yield 42
...
>>> result, = f()
>>> result
42
>>> result = f()[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'generator' object is not subscriptable

ChrisA

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web