<p dir="ltr"><br>
On 3 Feb 2013 00:21, "Steven D'Aprano" <<a href="mailto:steve@pearwood.info">steve@pearwood.info</a>> wrote:<br>
><br>
> On 03/02/13 00:06, Stefan Behnel wrote:<br>
>><br>
>> Hua Lu, 02.02.2013 08:38:<br>
>>>>>><br>
>>>>>> import ast<br>
>>>>>> ast.literal_eval("{ 'key': 'val' }")<br>
>>><br>
>>> {'key': 'val'}<br>
>>>>>><br>
>>>>>> ast.literal_eval("{ ('key',): 'val' }")<br>
>>><br>
>>> {('key',): 'val'}<br>
>>>>>><br>
>>>>>> ast.literal_eval("{ frozenset({'key'}): 'val' }")<br>
>>><br>
>>> Traceback (most recent call last):<br>
>>>    File "<stdin>", line 1, in<module><br>
>>>    File "/usr/lib/python3.3/ast.py", line 86, in literal_eval<br>
>>>      return _convert(node_or_string)<br>
>>>    File "/usr/lib/python3.3/ast.py", line 63, in _convert<br>
>>>      in zip(node.keys, node.values))<br>
>>>    File "/usr/lib/python3.3/ast.py", line 62, in<genexpr><br>
>>>      return dict((_convert(k), _convert(v)) for k, v<br>
>>>    File "/usr/lib/python3.3/ast.py", line 85, in _convert<br>
>>>      raise ValueError('malformed node or string: ' + repr(node))<br>
>>> ValueError: malformed node or string:<_ast.Call object at 0x7f865a8c1450><br>
>><br>
>><br>
>> So, why exactly are you using ast.literal_eval() to evaluate a non-literal<br>
>> expression?<br>
><br>
><br>
><br>
> Stefan, you did ask Hua Lu to show an example of what isn't working. It's just a demonstration of what doesn't work -- you can't create a frozenset using ast.literal_eval.<br>
><br>
> I think Hua Lu's original post made it quite clear. He wishes there to be a frozenset literal, because currently there is no way to have ast.literal_eval evaluate something containing a frozenset. Because there is no frozenset literal.<br>

><br>
> I think Raymond Hettinger's proposal back in 2008:<br>
><br>
> <a href="http://mail.python.org/pipermail/python-3000/2008-January/011798.html">http://mail.python.org/pipermail/python-3000/2008-January/011798.html</a><br>
><br>
> and the following thread is worth reading. Guido even pronounced his agreement:<br>
><br>
> <a href="http://mail.python.org/pipermail/python-3000/2008-January/011814.html">http://mail.python.org/pipermail/python-3000/2008-January/011814.html</a><br>
><br>
> but then changed his mind (as did Raymond). So the status quo remains.<br>
><br>
> Unfortunately the proposal to use f{ ... } for frozen sets cannot work within the constraints of Python's lexer:<br>
><br>
> <a href="http://mail.python.org/pipermail/python-3000/2008-January/011838.html">http://mail.python.org/pipermail/python-3000/2008-January/011838.html</a></p>
<p dir="ltr">To clarify Guido's comment in that post, I'm fairly sure it *can* be made to work, it just won' t be the same way that string prefixes work (because the contents of dict and set displays are not opaque to the compiler the way string contents are).</p>

<p dir="ltr">The hypothetical "What if we want to allow expr{} as a general construct?" objection needs to be balanced against the immediate value of a more expressive language subset for use in ast.literal_eval().</p>

<p dir="ltr">Cheers,<br>
Nick.</p>
<p dir="ltr">><br>
> 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.<br>

><br>
><br>
><br>
> -- <br>
> Steven<br>
><br>
> _______________________________________________<br>
> Python-ideas mailing list<br>
> <a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
> <a href="http://mail.python.org/mailman/listinfo/python-ideas">http://mail.python.org/mailman/listinfo/python-ideas</a><br>
</p>