>>> import ast
>>> ast.literal_eval("{ 'key': 'val' }")
{'key': 'val'}
>>> ast.literal_eval("{ ('key',): 'val' }")
{('key',): 'val'}
>>> ast.literal_eval("{ frozenset({'key'}): 'val' }")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.3/ast.py", line 86, in literal_eval
    return _convert(node_or_string)
  File "/usr/lib/python3.3/ast.py", line 63, in _convert
    in zip(node.keys, node.values))
  File "/usr/lib/python3.3/ast.py", line 62, in <genexpr>
    return dict((_convert(k), _convert(v)) for k, v
  File "/usr/lib/python3.3/ast.py", line 85, in _convert
    raise ValueError('malformed node or string: ' + repr(node))
ValueError: malformed node or string: <_ast.Call object at 0x7f865a8c1450>

On Sat, Feb 2, 2013 at 1:34 AM, Stefan Behnel <stefan_ml@behnel.de> wrote:
Hua Lu, 02.02.2013 07:24:
> This is similar to another proposal:
> http://mail.python.org/pipermail/python-3000/2008-January/011798.html
> Anyway, I was using ast.literal_eval and attempted to use frozenset({...})
> as a key in a dictionary, which failed, because frozenset isn't a literal

This has nothing to do with being a literal or not. The way you created
your frozenset doesn't impact its behaviour.

Could you give an example of what's not working for you? Frozensets as dict
keys work just fine for me:

  Python 3.2.3 (default, Oct 19 2012, 19:53:16)
  [GCC 4.7.2] on linux2
  Type "help", "copyright", "credits" or "license" for more information.
  >>> frozenset({1,2,3})
  frozenset({1, 2, 3})
  >>> hash(frozenset({1,2,3}))
  >>> { frozenset({1,2,3}) : 1 }
  {frozenset({1, 2, 3}): 1}


Python-ideas mailing list