[issue14836] Add next(iter(o)) to set.pop, dict.popitem entries.

2012-05-20 Thread Raymond Hettinger

Changes by Raymond Hettinger :


--
resolution:  -> rejected
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue14836] Add next(iter(o)) to set.pop, dict.popitem entries.

2012-05-20 Thread Terry J. Reedy

Terry J. Reedy  added the comment:

Raymond, I pretty much agree with your points and would be happy either with 
rejection or one simple sentence.  This idiom really belongs in a hypothetical 
how-to, such as 'Python iterators and generators'. The real use case for 
it=iter() followed by next(it) it to treat the first item of a collection 
specially before scanning the rest in a for loop.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue14836] Add next(iter(o)) to set.pop, dict.popitem entries.

2012-05-20 Thread Raymond Hettinger

Raymond Hettinger  added the comment:

I don't think it is a good idea to expand to the pop/popitem docs this way.  
The text is not about what pop/popitem does, it is about what another 
hypothetical method might do.  The text would be a distractor from the focused 
description of what pop/popitem actually do.  Also, it would face 
discoverability problems (there is no reason to think that someone who wants a 
nonmutating getfirst() method would think to look in the docs for a mutating 
method).

Also, the text talks about a general purpose programming technique (how to get 
the first element out of *any* iterable without removing it).  That belongs in 
a tutorial entry or a summary of idioms.

FWIW, the "several requests for set.get" haven't been serious requests 
accompanied by valid use cases.  Instead, they have mostly been toy discussions 
about all the ways you could do it ("for x in s: break",  "next(iter(s))", 
"x=s.pop(); s.add(x)").   The absence of get() or pick() in other language's 
set implementations suggest that there isn't a real need here.

That said, I don't think there is much of a downside to adding a sentence to 
the set.pop() docs.   It would be a waste though to also put it in dict.pop() 
and dict.popitem() where the question never seems to arise and where the docs 
already have issues with trying to over describe what can be done.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue14836] Add next(iter(o)) to set.pop, dict.popitem entries.

2012-05-18 Thread Raymond Hettinger

Changes by Raymond Hettinger :


--
assignee: docs@python -> rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue14836] Add next(iter(o)) to set.pop, dict.popitem entries.

2012-05-17 Thread Nadeem Vawda

Nadeem Vawda  added the comment:

> "popitem() 
> Remove and return an arbitrary (key, value) pair from the dictionary. Raises 
> KeyError if the dict is empty. Use next(iter(d)) to return an arbitrary pair 
> without removing it."

Actually, next(iter(d)) on a dict returns an arbitrary *key*; if you want
a pair, you need next(iter(d.items())).

--
nosy: +nadeem.vawda

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue14836] Add next(iter(o)) to set.pop, dict.popitem entries.

2012-05-16 Thread Éric Araujo

Éric Araujo  added the comment:

> Use next(iter(s)) to return an arbitrary element
I would suggest s/return/get/

IIUC calling this twice in a row will get the same element; should the doc 
mention that?

--
nosy: +eric.araujo

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue14836] Add next(iter(o)) to set.pop, dict.popitem entries.

2012-05-16 Thread Ned Deily

Changes by Ned Deily :


--
nosy: +rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue14836] Add next(iter(o)) to set.pop, dict.popitem entries.

2012-05-16 Thread Terry J. Reedy

New submission from Terry J. Reedy :

There have been several requests for a set.get() (no args) or set.pick() method 
to get an item without deleting it as .pop() does. Probably the best answer is 
to use the simple generic composition next(iter(s)). The counter response is 
that it is hardly obvious and needs to be documented.

Suggesion: after the current

"pop() 
Remove and return an arbitrary element from the set. Raises KeyError if the set 
is empty."

add "Use next(iter(s)) to return an arbitrary element without removing it."

Then change the following to match.

"popitem() 
Remove and return an arbitrary (key, value) pair from the dictionary.

popitem() is useful to destructively iterate over a dictionary, as often used 
in set algorithms. If the dictionary is empty, calling popitem() raises a 
KeyError."

to

"popitem() 
Remove and return an arbitrary (key, value) pair from the dictionary. Raises 
KeyError if the dict is empty. Use next(iter(d)) to return an arbitrary pair 
without removing it."

The old comment about destructively iterating over a dict as a set belongs more 
with set.pop if it is not removed.

The idiom works with all iterators, but there is no other place I can think of 
to put it, and it is not needed for sequences.

--
assignee: docs@python
components: Documentation
keywords: patch
messages: 160937
nosy: docs@python, terry.reedy
priority: normal
severity: normal
stage: needs patch
status: open
title: Add next(iter(o)) to set.pop, dict.popitem entries.
type: enhancement
versions: Python 2.7, Python 3.2, Python 3.3

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com