Search⌘ K

Solution Review: Updating Outer Scope Values

Explore how Python handles variable scope, especially when updating outer scope values. Understand the role of the global keyword in reassigning variables and how Python creates variables in global scope when assigned locally. This lesson helps you avoid common pitfalls related to variable scope and reassignment.

We'll cover the following...

Let’s look at the solution to the challenge in the previous lesson.

Solution

C++
#var = 9
def update_var(value):
global var
var = value
return var
print(update_var(6))
print(var)

Explanation

  • While we
...