[Python-ideas] get method for sets?

Masklinn masklinn at masklinn.net
Wed May 16 16:51:09 CEST 2012


On 2012-05-16, at 16:44 , Mike Graham wrote:

> On Wed, May 16, 2012 at 4:08 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
>> That's not an operation that applies just to sets, you can do it with
>> an iterable, therefore the spelling is one that works with any
>> iterable: next(iter(s))
> 
> It sounds like you're re-implementing the venerable ,= operator. ,= is
> one of my favorite operators in Python.
> 
> You know,
>>>> s
> set([42])
>>>> item ,= s
>>>> item
> 42

With the difference that ,= also asserts there is only one item in the
iterable, where `next . iter` only does `head`.

(but the formatting as a single operator is genius, I usually write it
as `item, = s` and the lack of clarity bothers me, thanks for that
visual trick)



More information about the Python-ideas mailing list