eval syntax problem

Josiah Carlson jcarlson at uci.edu
Wed Nov 17 13:29:47 EST 2004


Erik Max Francis <max at alcyone.com> wrote:
> 
> Matthias Teege wrote:
> 
> > what is wrong with
> > 
> > eval('print %s %s %s' % ('%s', '%', 'foo'))
> 
> print is a statement, not an expression, and eval only handles 
> expressions.  Use exec instead.

What the hell are you talking about?  Don't use either eval or exec,
both are remote holes waiting to happen.

Furthermore...

>>> print %s % foo
  File "<stdin>", line 1
    print %s % foo
          ^
SyntaxError: invalid syntax

> > fmt='%.2f'
> > a=1
> > b="fmt %" % a

That is also incorrect syntax.  Seemingly you want to be doing the
equivalent of...

b = '%.2f'%a
Which will just create a string representation of a float to 2 decimal
places of precision.

If you want to do rounding on the object, use round...

b = round(a, 2)


 - Josiah




More information about the Python-list mailing list