From the course: Python Essential Training
Unlock the full course today
Join today to access over 24,700 courses taught by industry experts.
Variables and scope - Python Tutorial
From the course: Python Essential Training
Variables and scope
Welcome, class. Today we're covering global and local variable scope interaction in Python. Jeez, 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 a function using asterisk args and asterisk asterisk quargs. 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. We're going to print the output of this locals function that's built into Python. And we can call this performOperation, one comma two, and let's call the operation multiply. Okay. And what we get is a dictionary of all the variables that have been passed in, whether they're positional arguments or keyword…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.