Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Evaluating Expressions

00:00 In the previous lesson, I showed you about the globals and locals parameters to eval(). In this lesson, I’m going to show you more details about the kinds of expressions you can use with eval().

00:11 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, and, or, and not; membership tests, in and not in; as well as identity, is and is not.

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.

01:13 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.

01:50 Passing in "a > b" gives you the subtraction.

01:58 "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 eval().

02:14 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.

02:27 You’ve seen simple expressions like addition before, or powers, but a lot of the more interesting math functions are inside the math library.

02:37 You can’t do an import inside of eval() itself, but eval() is sensitive to things that have been imported before its execution.

02:47 So once math has been imported, I can do things like the area of a circle,

02:56 the volume of a sphere,

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.

03:38 The challenge is things like the getoutput() function from subprocess. 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.

04:02 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.

04:13 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.