[Python-ideas] get method for sets?

Bruce Leban bruce at leapyear.org
Wed May 16 17:06:12 CEST 2012


On Wed, May 16, 2012 at 1:46 AM, Mike Meyer <mwm at mired.org> wrote:

> On Wed, 16 May 2012 01:08:09 -0700
> Bruce Leban <bruce at leapyear.org> wrote:
> > On Wed, May 16, 2012 at 12:40 AM, Mike Meyer <mwm at mired.org> wrote:
>
> I didn't claim it was fast. I actually wrote that version instead of the
> > in/return version for a very specific reason: it always returns the same
> > element. (The for/in/return version might return the same element every
> > time too but it's not guaranteed.)
>
> I didn't ask for a get that would always return the same element.
>

You didn't ask for a get that *didn't* always return the same element. My
deterministic version is totally compatible with your ask here and


> Would you also complain that having int accept a string value in lieu
> of using eval on untrusted input is a case of "it's useful anytime you
> need it."?
>

Not at all. It's useful because it's very common to need to convert strings
to numbers and I can show you lots of code that does just that. So we need
a method that does that safely. Does it have to be int? No; it could be
atoi or parse_int or scanf. But we do need it.

>
> > I don't think your test is very good if it uses the get I wrote
> > above. Your test will only operate on one element of the set and
> > it's easy to write functions which succeed for some elements of the
> > set and fail for others.  I'd like to see an actual test that you
> > think needs this that would not be improved by iterating over the
> > list.
>
> Talk about tautologies! Of course you can write tests that will fail
> in some cases. You can also write tests that won't fail for your
> cases. Especially if you know something about the set beforehand.
>

Not what I said. It's easy to write a *function* that fails on some
elements and your *test* won't test it. Example: a function that fails when
operating on non-integer set elements or the largest element or .... Your
test only tests an one case.

>
> For instance, I happen to know I have a set of ElementTree elements
> that all have the same tag. I want to check the tag.
>

Then maybe you should be using a different data structure than a set. Maybe
set_with_same_tag that declares that constraint and can enforce the
constraint if you want.


> One of the test cases starts by checking to see if the set is a
> singleton. Do you really propose something like:
>
>    if len(s) == 1:
>        for i in s:
>            res = process(i)
>
> This is a legitimate use case. I don't think it's a big deal to have to
add a one line function to your code. I might even use EAFP:

res = process(set_singleton(s))


where

def set_singleton(s):
    [result] = s
    return result


--- Bruce
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120516/908b4e92/attachment.html>


More information about the Python-ideas mailing list