In the previous lesson, I showed you about the
locals parameters to
eval(). In this lesson, I’m going to show you more details about the kinds of expressions you can use with
Not surprisingly, Boolean expressions are considered expressions, so
eval() can evaluate them. This includes all kinds of operators: value comparisons, less than (
<), greater than (
>), et cetera; logical comparisons,
not; membership tests,
not in; as well as identity,
01:34 Obviously, this is a little contrived. It’s not quite real-world, but you get the idea. The condition is being passed in and you’re changing the behavior of the function based on that condition passed in. Let me show you this in practice.
"a < b" gives you the addition. And of course, similarly with identity. A lot of the examples I’ve been using so far have to do with math, and this is because this is a very common use of
The challenge is things like the
getoutput() function from
getoutput() runs something on your machine outside of Python. Here, I’m showing you how to use
eval() to do
echo in Bash, or launch Firefox, or format your hard drive, or send horrible messages to your friends—any of those things.
Become a Member to join the conversation.