
I think dict.popitem() does something close to what the original post wanted. http://docs.python.org/3.1/library/stdtypes.html#dict.popitem On Fri, Sep 4, 2009 at 4:17 PM, Nick Coghlan<ncoghlan@gmail.com> wrote:
Matteo Dell'Amico wrote:
The shorthand expression above also suffers from the obscurity that Stefan was complaining about - there is very little to hint that "next(iter(obj))" means "get an arbitrary object out of a container". The StopIteration exception this approach will throw for an empty container is also rather unhelpful.
Why? next(iter(obj)) means, pretty explicitly to me, "iterate on obj and give me one element".
Because it overspecifies the semantics of what you're trying to do. It just happens that when the requirement is "get me any object in this container" the design of Python means that the easiest implementation is "get me the first object in this container".
The expression form then reflects the implementation rather than the algorithmic intent.
That said, this concise way of implementing the desired feature is certainly one of the reasons I am -0 on the idea of adding it to the standard library.
Cheers, Nick.
-- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia --------------------------------------------------------------- _______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas