From the course: Python Essential Training

Unlock the full course today

Join today to access over 24,500 courses taught by industry experts.

Variables and scope

Variables and scope

- Welcome class. Today we're covering global and local variable scope interaction in Python. - Geez, that sounds tedious. Ignore them. I promise there's nothing crazy to memorize here. Just watch, learn, get a feel for it. It's easy. Previously, we saw how we could print out the arguments passed into function using asterisk args and asterisk asterisk kwargs. So you see it prints out that tuple and a dictionary there. But there's another way we can get access to all the variables in a Python function without using any asterisks at all, and that's the locals function. So let's rewrite our original function definition, perform operation num1, num2, operation equals by default sum. And we're going to print the output of this locals function that's built into Python, and we can call this perform operation one comma two, and let's call the operation multiply, okay? And what we get is a dictionary of all the variables…

Contents