Local and global names
🌍 In real life. In a school, the notice board in the main hall is for everyone: any class can read it. Each classroom also has its own whiteboard. What a teacher writes on Class 7's whiteboard is only for that room, and it's wiped clean when the lesson ends. If two rooms both write "Test on Monday" on their whiteboards, they don't clash.
Python keeps names the same way: a shared board for the whole program, and a private one for each function call.
The idea in one sentence: names made outside any function are global, names made inside a function call are local to that call, and Python looks for a name in the local frame first, then the global one.
Its words:
- scope: where in the program a name can be seen
- global frame: the names made at the top level of the program
- local frame: a fresh set of names for one function call, gone when it returns
- lookup order: local first, then global (then Python's built-ins, like
print)
Step by step, for the code below:
x = 1andy = 3go in the global frame.f(5)makes a local frame witha = 5;x = 2inside f makes a local x.- Inside f,
aandxare found locally, andyis found globally. - f returns, its frame disappears, and
print(x)outside shows the global 1.
x = 1
y = 3
def f(a):
x = 2
print(a, x, y)
f(5)
print(x)When Python meets a name inside a function, it looks in the local frame first, then in the global frame. A local name is gone once the function returns, so using it outside is an error:
def make():
secret = 42
return secret
print(make())
print(secret)Why it works this way. Each function has its own private names, so a variable called total in one function can never clash with a total somewhere else. You can read and fix one function without worrying about every name in the whole program. And because a call's frame disappears when it returns, the memory it used is freed.
Mistakes and edge cases.
- Using a local name outside.
print(secret)aftermake()givesNameError: name 'secret' is not defined(above). - Expecting an assignment inside to change the global.
x = 2inside f makes a new local x; the global x is untouched. - Reading is fine, changing a list is too. A function can read a global, and
x.append(2)changes a global list (it changes the object, not the name). Onlyx = ...makes a local. - The same name for a parameter and a global. Inside the function, the parameter wins; that's usually fine, but it can confuse a reader.
Quiz
What does this print?
x = 10 def f(): x = 99 return x print(f(), x)What does this print?
name = "Ana" def greet(): return "Hi " + name print(greet())Where does Python look first for a name used inside a function?
- In the function's local frame
- In the global frame
- In the last function that ran
What does this print?
def add_one(n): n = n + 1 return n n = 5 print(add_one(n), n)Which line has a mistake?
def area(w, h): result = w * h return result area(3, 4) print(result)
Coding tasks
There's a global
count = 10. Writeshow_count()that returns the globalcount.Write
make_greeting(name)that puts"Hello, " + namein a local variable calledmsgand returns it. No globalmsgshould exist.There's a global
x = 5. Writebump()that returnsx + 1without changing the globalx.