Simple eval

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Nov 19 03:12:35 EST 2007


En Sun, 18 Nov 2007 22:24:39 -0300, greg <greg at cosc.canterbury.ac.nz>  
escribi�:

> 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