[Python-ideas] frozenset literals

Antoine Pitrou solipsis at pitrou.net
Sun Feb 3 10:04:55 CET 2013


On Sun, 3 Feb 2013 14:50:45 +1000
Nick Coghlan <ncoghlan at gmail.com> wrote:
> 
> This is why we need a PEP or a PyPI module. It's certainly not clear
> to me that special casing in ast.literal_eval (or a new
> "ast.limited_eval") is a superior solution to s{} and fs{} syntax for
> creating the empty set and frozen sets.

Well, it's superior because it doesn't need new syntax.

> (And, as Raymond notes, there
> are other compile-time benefits in terms of constant-caching when it
> comes to dedicated syntax)

Which is a rather dubious benefit when it comes to a little-used
datatype. If you want to avoid the cost of instantiating a frozenset in
every loop iteration, just hoist it manually out of the loop.

> On the other hand, a "limited_eval" style solution might be easier to
> extend to other builtins like range, reversed and enum, as well as to
> container comprehensions and generator expressions. It also has the
> virtue of being possible to write as a PyPI module, and made available
> for *current* Python versions, rather than only being available in
> Python 3.4+.

Agreed.

Regards

Antoine.





More information about the Python-ideas mailing list