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,
00:34 To demonstrate some Boolean expressions, let me create some variables.
00:43 As you would expect, the result of Boolean comparisons is a Boolean value. It also supports compounds. And here is identity. An example of membership. So far, this is pretty vanilla.
01:02 Let me show you why you might want to be able to do this. Consider the situation where you have a function whose behavior needs to change based on some sort of condition passed in.
You can pass that condition in as a string and then use
eval() to evaluate that condition, changing the behavior of the function. Let me show you an example.
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 subtraction.
"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
It’s like having a little calculator built into Python. Pretty much any arbitrary math expression can be evaluated, as long as you remember to import the
math library first.
You’ve seen simple expressions like addition before, or powers, but a lot of the more interesting math functions are inside the
You can’t do an import inside of
eval() itself, but
eval() is sensitive to things that have been imported before its execution.
math has been imported, I can do things like the area of a circle,
03:03 or I can get my Pythagoras on.
03:11 Brilliant man. A little crazy, but brilliant man. Go look him up. He had a thing about beans. It was weird. Anything that is a function in Python is actually considered an expression.
03:25 That is where you run into some problems. Thankfully, Lost in Space has been rebooted, and so I’m no longer making an esoteric reference to a 1950s black and white television show. I’m all hip.
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.
This is why you want to be very careful what you pass into
eval() and where you got it from. I know I’ve warned you before and I will warn you again because it’s important.
So far, I’ve only been using strings with
eval(). You can also use pre-compiled objects. In the next lesson, I’ll demonstrate that.
Become a Member to join the conversation.