[Python-ideas] get method for sets?
Cameron Simpson
cs at zip.com.au
Thu May 17 02:56:29 CEST 2012
On 16May2012 17:42, Stephen J. Turnbull <stephen at xemacs.org> wrote:
| Mike Meyer writes:
| > On Wed, 16 May 2012 17:26:45 +1000
|
| > > Could this helper function not do the job?
| > >
| > > def get(s):
| > > x = s.pop()
| > > s.add(x)
| > > return x
| >
| > Sure, if you don't mind munging the set unnecessarily. That's more
| > readable, but slower and longer than:
| >
| > def get(s):
| > for x in s:
| > return s
I was about to suggest Mike's implementation.
| Why would you mind munging the set temporarily?
Personally, I work with multiple threads quite often. Therefore I
habitually avoid data structure modifying operations unless they're
neccessary. Any time I modify a data structure is a time I have to worry
about shared access.
| Why is speed (of
| something that almost by definition is undefined if repeated)
| important?
Besides, modifying a data structure _is_ slow than just looking,
usually. There may even be garbage collection:-(
| I'm -1 on adding a method that has no motivation in production that I
| can see. Just redefine your get() function as a function, with a more
| appropriate name such as "get_item_nondeterministically". It will
| work on any iterable. (Don't forget to document that it will "use up"
| an item if the iterable is not a sequence, though.)
Yah:
def an(s):
for i in s:
return i
I'm also -1 on a set _method_, though he can always subclass and add his
own for his use case.
Cheers,
--
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/
If your new theorem can be stated with great simplicity, then there
will exist a pathological exception. - Adrian Mathesis
More information about the Python-ideas
mailing list