meaning of: line, =

Ethan Furman ethan at stoneleaf.us
Wed Feb 4 12:09:59 EST 2015


On 02/04/2015 07:04 AM, Chris Angelico wrote:
> On Thu, Feb 5, 2015 at 1:38 AM, Albert-Jan Roskam <fomcl at 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;

By unpacking you are also double checking that the returned iterable contains exactly one item; more or less will cause
an exception -- you only get half that check if you index.

--
~Ethan~

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20150204/72fa47c8/attachment.sig>


More information about the Python-list mailing list