Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Bugs

[issue7212] Retrieve an arbitrary element from a set without removing it

 

 

Python bugs RSS feed   Index | Next | Previous | View Threaded


report at bugs

Oct 26, 2009, 2:07 PM

Post #1 of 10 (394 views)
Permalink
[issue7212] Retrieve an arbitrary element from a set without removing it

New submission from Willi Richert <w.richert [at] gmx>:

Sometimes, a non-removing pop() is needed. In current Python versions,
it can achieved by one of the following ways:

1.
x = some_set.pop()
some_set.add(x)

2.
for x in some_set:
break

3.
x = iter(some_set).next()

More native and clean would, however, be
some_set.get()

The attached patch does this for set(). If this is accepted by the
community, frozenset should be extended as well.

----------
components: Library (Lib)
files: setobject_get.patch
keywords: patch
messages: 94508
nosy: wrichert
severity: normal
status: open
title: Retrieve an arbitrary element from a set without removing it
type: feature request
versions: Python 3.1, Python 3.2
Added file: http://bugs.python.org/file15207/setobject_get.patch

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7212>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Oct 26, 2009, 2:12 PM

Post #2 of 10 (370 views)
Permalink
[issue7212] Retrieve an arbitrary element from a set without removing it [In reply to]

Changes by Raymond Hettinger <rhettinger [at] users>:


----------
assignee: -> rhettinger
nosy: +rhettinger
versions: +Python 2.7 -Python 3.1

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7212>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Oct 26, 2009, 2:22 PM

Post #3 of 10 (360 views)
Permalink
[issue7212] Retrieve an arbitrary element from a set without removing it [In reply to]

Benjamin Peterson <benjamin [at] python> added the comment:

Without tests, this patch is unacceptable.

----------
nosy: +benjamin.peterson

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7212>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Oct 27, 2009, 2:07 AM

Post #4 of 10 (352 views)
Permalink
[issue7212] Retrieve an arbitrary element from a set without removing it [In reply to]

Changes by Willi Richert <w.richert [at] gmx>:


Removed file: http://bugs.python.org/file15207/setobject_get.patch

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7212>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Oct 27, 2009, 2:08 AM

Post #5 of 10 (354 views)
Permalink
[issue7212] Retrieve an arbitrary element from a set without removing it [In reply to]

Changes by Willi Richert <w.richert [at] gmx>:


Added file: http://bugs.python.org/file15211/setobject_get.patch

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7212>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Oct 27, 2009, 2:09 AM

Post #6 of 10 (357 views)
Permalink
[issue7212] Retrieve an arbitrary element from a set without removing it [In reply to]

Willi Richert <w.richert [at] gmx> added the comment:

added tests for get() to test_set.py

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7212>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Oct 27, 2009, 6:56 PM

Post #7 of 10 (340 views)
Permalink
[issue7212] Retrieve an arbitrary element from a set without removing it [In reply to]

Alexander Belopolsky <belopolsky [at] users> added the comment:

Any reason you don't want to call set_next from set_get?

I would say

static PyObject *
set_get(PySetObject *so)
{
register Py_ssize_t pos = 0;
register setentry *entry;
if (set_next(so, &pos, &entry)) {
Py_INCREF(entry->key);
return entry->key;
}
/* set appropriate error */
return NULL;
}

BTW, what your patch is supposed to do on set().get()?
}

----------
nosy: +belopolsky

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7212>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Oct 28, 2009, 1:13 AM

Post #8 of 10 (343 views)
Permalink
[issue7212] Retrieve an arbitrary element from a set without removing it [In reply to]

Willi Richert <w.richert [at] gmx> added the comment:

No particular reason, besides that it is ripped off of pop().

Your solution (omitting "register") gives the same performance. Looks
cleaner, of course.

The patch tries to provide a clean way of "for x in some_set: break", as
explained above. See the recent python-dev mailing list musings.

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7212>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 5, 2009, 10:42 AM

Post #9 of 10 (303 views)
Permalink
[issue7212] Retrieve an arbitrary element from a set without removing it [In reply to]

Raymond Hettinger <rhettinger [at] users> added the comment:

After a long discussion on python-dev, this proposal is rejected in
favor of adding documentation notes on the ways to non-destructively
retrieve an arbitrary item from a set or frozenset.

Here is an except from the end of the thread:

[Steven D'Aprano]
>> Anyway, given the level of opposition to the suggestion, I'm no longer
>> willing to carry the flag for it. If anyone else -- perhaps the OP --
>> feels they want to take it any further, be my guest.

[geremy condra]
> I've said before that I'd like there to be one, standard way of
> doing this. A function call- set.pick() seems reasonably named
> to me- is probably the cleanest way to do that. Absent that,
> an example in the docs that illustrates the preferred idiom
> would be great.

[Raymond]
Summarizing my opposition to a new set method:
1) there already are at least two succinct ways to get the same effect
2) those ways work with any container, not just sets
3) set implementations in other languages show that this isn't needed.
4) there is value to keeping the API compact
5) isn't needed for optimization (selecting the same value in a loop
makes no sense)
6) absence of real-world code examples that would be meaningfully improved

[Terry Reedy]
Agreed

[Raymond]
I would be happy to add an example to the docs so that this thread
can finally end.

[Eric Smith]
Please do!

[Terry Reedy]
Yes!
'''


Leaving this open until I've done the documentation patch.

----------
priority: -> low
resolution: -> rejected

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7212>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 5, 2009, 10:57 AM

Post #10 of 10 (301 views)
Permalink
[issue7212] Retrieve an arbitrary element from a set without removing it [In reply to]

Alexander Belopolsky <belopolsky [at] users> added the comment:

I don't want to pollute python-dev with more hopeless ideas, but I wonder
if itertools could grow an efficient C-implemented

def first(collection):
return next(iter(collection))

On the other hand, it probably belongs to recipes more than stdlib. This
is not really an iterator tool after all.

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7212>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com

Python bugs RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.