[Python-ideas] frozenset literals

Steven D'Aprano steve at pearwood.info
Sun Feb 3 03:18:20 CET 2013


On 03/02/13 05:30, MRAB wrote:
> On 2013-02-02 14:28, Antoine Pitrou wrote:
>> On Sun, 03 Feb 2013 01:20:38 +1100
>> Steven D'Aprano <steve at pearwood.info> wrote:
>>>
>>> Unfortunately the proposal to use f{ ... } for frozen sets cannot work within the constraints of Python's lexer:
>>>
>>> http://mail.python.org/pipermail/python-3000/2008-January/011838.html
>>>
>>> Unfortunately we're running out of useful, easy to enter symbols for literals. Until such time (Python4000 perhaps, or more likely Python5000) as we can use a rich set of Unicode literals, I don't think there is any clear way to have a frozenset literal.
>>
>> I'm not sure what the point is. frozensets are too rarely used to
>> deserve a literal notation.
>>
> Does it need a special notation as such?
>
> Wouldn't an alternative be to support "frozenset(...)" where "..." is
> parseable by ast.literal_eval()?
>
> The same thing could be done with some other built-in classes.


I think that having literal_eval support non-literals is a bad, bad idea. Let's just not go there. It will surely end in tears.

However, I think that having something in between the strictness of literal_eval and the dangerous "anything goes" power of eval is a good idea. For a long time now I've toyed with an engine for building expression evaluators. Something that understands operators, function calls, etc, and you can tell it what names to accept. My main motivation is for evaluating mathematical expressions like:

5x^3 - 2x + log(1/y) + n!/√π

I have some old, broken Pascal code that half does what I want, and I keep intending to revisit it some day in Python. In my copious spare time.

In any case, I think something like that belongs in a third-party module, at least at first.



-- 
Steven



More information about the Python-ideas mailing list