[Python-ideas] frozenset literals

Hua Lu gotoalanlu at gmail.com
Sun Feb 3 21:45:14 CET 2013


> It looks like we can special-case usage of set literal as a key for a dict
> or a member for another set and create a frozenset constant instead.
>
> So that adict[{1, 2, 3}] should be interpreted as adict[frozenset([1, 2,
> 3])] and { {'b', 'a', 'r'}, 'foo' } as { frozenset('bar'), 'foo' }

This could interfere with the behavior of types overriding the index
operator. Also, it's a special case.

> This will provide a minimal change to the interpreter while making it
> possible to use any literal-parsing with frozensets while keeping method
> calls out of literal_eval.

Why shouldn't we change the interpreter when it could make the language better?

Honestly, I can't think of an element which will require special
syntax like symbol{...}. Furthermore, it could be really confusable
with current syntax. Example:

    for fn { bar(foo) for foo in baz }:

What if bar was a higher order function and that syntax meant
something like "make a generator applying each thing in the set to an
original fn before introducing fn in the scope" when you really meant
`for fn in {...}`?

Python 3 has already made some calls more explicitly a function (e.g.
print) so having that as syntactic sugar for that would also be weird.
There doesn't seem to be any _use_ for syntax like symbol{...}, so I
feel for example s{...}, f{...}, and d{...} would be a natural
extension to Python insofar as b'...' and u'...' are in the language,
_even though_ sets and maps feel more general mathematically speaking
(so the need for special use syntax _may_ be inappropriate (but people
write math in languages ;)).



More information about the Python-ideas mailing list