[Python-ideas] More on set literals

VanL van.lindberg at gmail.com
Sat Jan 26 18:18:12 CET 2008


I like Raymond's suggestion (apparently adopted) that {1,2,3} is a 
frozenset literal. However, I also like the {{1,2,3}} syntax. So I would 
propose that {{}} become the literal for the *mutable* set, not the 
frozenset.

So:
frozenset() # empty frozenset
{1,2,3} # frozenset literal

{} # empty dict
{1:2, 3:4} #dict literal

{{}} (or set()) # empty mutable set
{{1,2,3}} # set literal

My rationale is as follows:
1. It visually distinguishes sets and frozensets, without making them 
take up a lot of space.  If people see the {{}}, they will be reminded 
that this set is mutable.

2. In Raymond's example, his point was that most people don't want a 
mutable literal - they would be better served by a frozenset.  However, 
sometimes you *do* want to add elements to a set literal.  For example:

# I am parsing the configuration file for a pure-python webserver.
# The config file allows me to add new filetype extensions
# that will be served.

# Default HTML extensions:
HTML_EXTS = {{'.html', '.htm'}}

# later, when going through config file:
if filetype.handler == HTMLHandler:
     HTML_EXTS.add(filetype.ext)

I know that this can be done other ways (set(['.html', '.htm'])), but I 
like the way this looks.

Thanks,

Van

P.S.: The bikeshed should be yellow.




More information about the Python-ideas mailing list