[Tutor] eval use (directly by interpreter vs with in a script)

Danny Yoo dyoo at hashcollision.org
Mon Nov 3 19:57:25 CET 2014

On Mon Nov 03 2014 at 10:48:29 AM Danny Yoo <dyoo at hashcollision.org> wrote:

> On Mon Nov 03 2014 at 10:04:41 AM Alan Gauld <alan.gauld at btinternet.com>
> wrote:
>> On 03/11/14 17:33, Albert-Jan Roskam wrote:
>> > I sometimes do something like
>> > ifelse = "'teddybear' if bmi > 30 else 'skinny'"
>> > weightcats = [eval(ifelse) for bmi in bmis]
>> >
>> > Would this also be a *bad* use of eval? It can be avoided, but this is
>> so concise.

> Consider if 'bmi' were ever controlled from the outside world.  At the
> very worst, the normal function approach will raise a runtime error, but
> that's it.  In contrast, the eval approach can take over your machine.

I should be a bit more careful in saying this.  I should have said:
"Consider if 'ifelse'" were ever controlled from the outside world...".  My
apologies for writing a bit too fast there.

In any case, the other comments still stand.  You want to use functions for
cases like the above.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20141103/a06ae035/attachment-0001.html>

More information about the Tutor mailing list