Simple eval

Gabriel Genellina gagsl-py2 at
Mon Nov 19 09:12:35 CET 2007

En Sun, 18 Nov 2007 22:24:39 -0300, greg <greg at>  

> Importing the names from tokenize that you use repeatedly
> should save some time, too.
>from tokenize import STRING, NUMBER
> If you were willing to indulge in some default-argument abuse, you
> could also do
>    def atom(next, token, STRING = tokenize.STRING, NUMBER =  
> tokenize.NUMBER):
>      ...
> A more disciplined way would be to wrap it in a closure:
>    def make_atom():
>      from tokenize import STRING, NUMBER
>      def atom(next, token):
>        ...
>      return atom

...but unfortunately it's the slowest alternative, so wouldn't count as a  
speed optimization.

I would investigate a mixed approach: using a parser to ensure the  
expression is "safe", then calling eval.

Gabriel Genellina

More information about the Python-list mailing list