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.
On 26 Jan 2008, at 12:18, VanL wrote:
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.
How would this be distinguished from a singleton frozenset containing another frozenset?
On Jan 26, 2008 9:43 AM, Christian Heimes
VanL wrote:
{{}} (or set()) # empty mutable set {{1,2,3}} # set literal
{{ }} is ambiguous, hard to read and only one to three characters shorter than set() / set({}). -1 from me
Right. This is not even on the table. -- --Guido van Rossum (home page: http://www.python.org/~guido/)
On 1/26/08, VanL
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.
As Adam Atlas has already pointed out, {{}} already has a meaning in
the newly proposed scheme.
But that's not really the point that needs to be made. The typical
response to this type of suggestion is to point out the obvious
technical flaw, and that's a shame, because I don't think that's the
real problem. You could have instead suggested, say, {|1, 2, 3|} as
the new syntax for set literals. That fixes the technical flaw, but
it still would have been, I believe, a bad idea.
I don't understand the instinct people have that every concept needs
to be generalized, that every non-uniformity in the language needs
fixing. Normally the target is adding statements to lambdas, but
today our bikeshed is the set literal syntax. There seems to be an
underlying assumption to these posts that I find irritiating -- that
the Python designers and developers would have added these features if
only they were smart enough to be able to think up a syntax for them
without assistance!
Raymond Hettinger's frozenset literal proposal is solid. It
recognizes that sets created from literal notation will typically not
be mutated, that set({1,2,3}) is not a burdensome syntax in the off
cases, and that literal frozensets admit a new and faster idiom for
comparing a value against a small set of constants.
The one flaw with his proposal was his prediction:
On 1/24/08, Raymond Hettinger
P.S. A small side-benefit is it may put an end for interminable requests for a {:} or {/} notation for empty sets. There's not much need for a literal for a empty frozenset (use "not s" instead).
It turned out to only take about two days. Greg F
participants (5)
-
Adam Atlas
-
Christian Heimes
-
Greg Falcon
-
Guido van Rossum
-
VanL