[Python-ideas] get method for sets?

Stephen J. Turnbull stephen at xemacs.org
Wed May 16 10:42:49 CEST 2012


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

Why would you mind munging the set temporarily?  Why is speed (of
something that almost by definition is undefined if repeated)
important?  Your example use case of testing doesn't motivate these
parts of your requirements.

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.)



More information about the Python-ideas mailing list