[Python-3000] sets in P3K?

Marcin 'Qrczak' Kowalczyk qrczak at knm.org.pl
Sat Apr 29 17:55:36 CEST 2006


Raymond Hettinger <rhettinger at ewtllc.com> writes:

> Good question.  To see the answer, look at a code tranformation from:
>
>     if file_ext.lower() in set(['html', 'xml', 'xhtml']):
>         handle(filename)
>
> into:
>
>
>     if file_ext.lower() in {'html', 'xml', 'xhtml'}:
>         handle(filename)

Beware that without optimization this:

      if file_ext.lower() in ['html', 'xml', 'xhtml']:
          handle(filename)

is probably faster than this:

      if file_ext.lower() in {'html', 'xml', 'xhtml'}:
          handle(filename)

and with some trivial optimization this:

      if file_ext.lower() in ('html', 'xml', 'xhtml'):
          handle(filename)

is even faster (the tuple can be allocated statically).

So from the efficiency point of view using sets in such cases is a loss.
And I'm not sure that optimizing 'x in {a,b,c}' to comparisons would be
justified: it gives different results if some of a,b,c are not hashable.

-- 
   __("<         Marcin Kowalczyk
   \__/       qrczak at knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/


More information about the Python-3000 mailing list