[Python-ideas] frozenset literals
Hua Lu
gotoalanlu at gmail.com
Fri Feb 8 03:34:30 CET 2013
Hey all, I have a simple hack around this problem for the time being.
It involves adding a parameter to ast.literal_eval. See this diff:
$ diff ast.py.bak ast.py
38c38
< def literal_eval(node_or_string):
---
> def literal_eval(node_or_string, use_frozensets=False):
49a50
> set_t = frozenset if use_frozensets else set
60c61
< return set(map(_convert, node.elts))
---
> return set_t(map(_convert, node.elts))
Use is as follows:
>>> import ast
>>> ast.literal_eval("{1,2,3}")
{1, 2, 3}
>>> ast.literal_eval("{1,2,3}", use_frozensets=True)
frozenset({1, 2, 3})
>>> ast.literal_eval("{{1,2,3}: 'foo'}", use_frozensets=True)
{frozenset({1, 2, 3}): 'foo'}
Regards,
Alan
More information about the Python-ideas
mailing list