[Python-3000] sets in P3K?
Raymond Hettinger
rhettinger at ewtllc.com
Sat Apr 29 00:13:54 CEST 2006
Brian Harring wrote:
>Potentially a stupid question, but I've been following this thread for
>a while and I'm still not seeing the real gain of extending the syntax
>for frozen markers/set literals.
>
>Someone care to recap what actually is gained from having a set
>literal?
>~harring
>
>
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)
The latter form builds a set directly and does not construct a temporary
intermediate list. Also, the latter form is potentially optimizable so
that the set is built at compile-time and referrenced as a constant
rather than being rebuilt from scratch everytime the code is executed.
In terms of user-friendliness, the latter form is closer to the way sets
are expressed in mathematics.
The only bump in the road is that the {} notation is so similar to
dictionaries that care must be taken to keep the two as distinct as
possible.
Raymond
More information about the Python-3000
mailing list