[Python-ideas] get method for sets?

Mike Graham mikegraham at gmail.com
Wed May 16 16:57:55 CEST 2012


On Wed, May 16, 2012 at 10:51 AM, Masklinn <masklinn at masklinn.net> wrote:
> 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)

1. I should have quoted Mike Meyer's code "if len(s) == 1: res =
process(next(iter(s)))", which is what I had in mind. (In that case,
it's a feature. :) )

2. I grouped it this way as a joke. If you do this, everyone will
think you're crazy. I've been known to write it (item,) = s, which
makes it a little easier to see the comma.

If you want to be unambiguous AND confuse everyone, go with

>>> s
set([42])
>>> [item] = s
>>> item
42

Mike



More information about the Python-ideas mailing list