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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Errors, Special Variables, and Reloading Imported Modules

00:00 Errors, Special Variables, and Reloading Imported Modules. When an error occurs in a REPL session, the interpreter automatically prints the corresponding error message and traceback.

00:12 Then it loops back to the REPL’s primary prompt. In the examples seen on-screen, you’ll see this behavior in action. Here, the string literal isn’t properly closed with a double quote (").

00:29 This raises a SyntaxError. This second example raises a ZeroDivisionError exception because you’re trying to divide 42 by 0. And finally, you call the built-in sum() function without arguments, which raises a TypeError.

00:48 All of these errors are immediately printed on the screen. This behavior allows you to quickly and carefully inspect the error message and traceback in order to track the underlying issue and fix your code.

01:03 Every time you run a statement that returns a value, the interpreter internally stores that value in a special variable named with a single underscore (_).

01:11 You can access and use this variable like any other variable in Python. On-screen are a couple of examples that show the implicit _ variable in action when you work with expressions.

01:24 Here you are evaluating expressions. They always have a return value, which is automatically assigned to the _ variable every time. When it comes to function calls, if the target function returns a value different from None, then _ will hold that value. In contrast, if the function returns None, then the _ variable will keep the value at the preceding operation.

01:51 The built-in pow() function computes the power of a number to a given exponent, returning the result. Because the function’s result differs from None, the _ variable is automatically reassigned.

02:04 In contrast, if you call a function that returns None, such as print(), then the _ variable remains unchanged.

02:17 Next, you use an assignment statement to create and initialize a counter variable. Assignments don’t return any value. Instead, they store a reference to a value in a variable. In this case, the _ variable isn’t updated after running the statement.

02:37 That’s why the variable still contains the number 16 from the previous examples. Note that accessing a variable in an interactive session returns the value referenced by that variable. In this case, that value is also assigned to the _ variable.

02:59 Because _ is a regular Python variable, you can use it in expressions and statements. Here you first create a list of values. Then you call len() to get the number of values in the list.

03:15 Python automatically stores this value in underscore (_). Finally, you use _ to compute the mean of your list of values.

03:28 When using the _ variable in a REPL session, keep in mind that this variable only appears in interactive mode. If you run your code in script mode, then you won’t get this implicit behavior.

03:42 Let’s say you’re writing a Python module with some functions for one of your projects. At the same time, you are using Python in interactive mode to test the module’s code in real time.

03:54 Let’s say you have the modules seen on-screen. Here you define a greet() function that prints a greeting message on the screen. Next, on-screen, you’ll see how to load and use this code in a REPL session.

04:22 Now say that you want to add a new argument to your function. The argument will be a Boolean flag that allows printing the greeting message in uppercase letters. To do this, you may modify it in the ways seen on-screen.

04:54 This update allows you to call greet() with an upper argument. If you set the argument to True, then the message will be in uppercase letters.

05:06 Now you want to try your changes in the current REPL session, so you import the module again, hoping to run the new version of greet() as seen.

05:22 But why do you have an unexpected argument? For efficiency reasons, running the import, again after updating something in greeting.py doesn’t reload the module. Python doesn’t load imported modules again when you rerun the import.

05:38 If you want to work around this behavior without closing your current REPL session and opening a new one, then you can use the reload() function from importlib.

06:00 Now the function works. The reload() function has taken care of loading the new version of the greeting module. You can use this trick whenever you’re working on a module in your code editor and testing your changes in a REPL session.

06:18 Now that you’ve learned the basics of entering and executing code in a Python interactive shell, it’s time to explore and learn about a few editing features that the standard REPL provides.

Become a Member to join the conversation.