Python 3D Academyবাংলা

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:

  1. x = 1 and y = 3 go in the global frame.
  2. f(5) makes a local frame with a = 5; x = 2 inside f makes a local x.
  3. Inside f, a and x are found locally, and y is found globally.
  4. 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)
Inside f, x = 2 makes a new local x; the global x stays 1. y isn't local, so f reads the global y.

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) after make() gives NameError: name 'secret' is not defined (above).
  • Expecting an assignment inside to change the global. x = 2 inside 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). Only x = ... 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

  1. What does this print?

    x = 10
    
    def f():
        x = 99
        return x
    
    print(f(), x)
  2. What does this print?

    name = "Ana"
    
    def greet():
        return "Hi " + name
    
    print(greet())
  3. 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
  4. What does this print?

    def add_one(n):
        n = n + 1
        return n
    
    n = 5
    print(add_one(n), n)
  5. Which line has a mistake?

    def area(w, h):
        result = w * h
        return result
    
    area(3, 4)
    print(result)

Coding tasks

  1. There's a global count = 10. Write show_count() that returns the global count.

  2. Write make_greeting(name) that puts "Hello, " + name in a local variable called msg and returns it. No global msg should exist.

  3. There's a global x = 5. Write bump() that returns x + 1 without changing the global x.