Easy(?) newbie question

Jeff Shannon jeff at ccvcorp.com
Mon Nov 12 17:04:14 EST 2001


Ron Stephens wrote:

> In other words, exec is a statement, but eval and execfile are functions. Since
> eval is a function, it doesn't make sense to give it a for loop. Try using exec
> instead, I think it will work like you expect.
>
> > On Sun, Nov 11, 2001 at 10:16:38PM +0000, Erik Johnson wrote:
> > >
> > > but when I try to put this code into a string and evaluate it, I get a
> > > syntax error:
> > >
> > > >>> s = "for x in range(10): print x,"
> > > >>> s
> > > 'for x in range(10): print x,'
> > > >>> eval(s)

As another minor bit of advice...  exec and eval() should be considered to be
dangerous magic--there can be many unexpected results from using these functions, and
they should *not* be recommended for newbies, and rarely for experienced coders.  They
can be essential in a few special circumstances, but for most of the things you may be
tempted to use them for, they will be far more of a headeache than a benefit.

In other words, try to forget about exec and eval() for a few more months at least.
;)

Jeff Shannon
Technician/Programmer
Credit International





More information about the Python-list mailing list