[Python-3000] Set literal

John Barham jbarham at gmail.com
Fri Jan 25 01:32:48 CET 2008

On Jan 24, 2008 4:23 PM, Barry Warsaw wrote:

> On Jan 24, 2008, at 7:12 PM, Raymond Hettinger wrote:
> >> Looking over the code base, frozensets are used rarely.
> >> So I don't think this is warranted.
> >
> > There is no shortage for perfect use cases in the form:
> >
> >   if urlext in {'html', 'xml', 'php'}:
> It's interesting that you point this out because I've had debates with
> people about the difference between:
>      if urlext in ['html', 'xml', 'php']:
> and
>      if urlext in ('html', 'xml', 'php'):
> I generally prefer the latter but I know folks who prefer the former.
> What's interesting is that in both cases we're trying to ask if the
> key is a member of a literal set, and to me, it makes no sense for
> that literal to be mutable.  So I also think Raymond's idea has merit.

Well I've measured the time difference and, at least in CPython on my
machine, constructing small tuples is quicker than constructing small
lists, although not enough to make any significant runtime difference.

But getting back to the original issue, what does using frozensets
gain you over using a tuple:

if urlext in ('html', 'xml', 'php'):

Given that tuples are also immutable, couldn't the peepholer also
optimize the tuple as a compile time constant?  Since this idiom is
usually used as a shortcut for:

if urlext == 'html' or urlext == 'xml' or urlext == 'php':

the semantics of using a frozenset are subtly different since with the
equivalent tuple you know the search is linear and short circuits on
success, but using a frozenset the search order is undefined.  Of
course if you had a largish search set, frozenset would probably be
quicker than using a tuple, but I think it's safe to say the search
set is small for the typical case.


More information about the Python-3000 mailing list