A name can be used only where Python can see it.
Concepts 15–17 followed a function call from definition through input and return. Now ask where the names used during that call are available. Use this beginner model: name → where it is created → where it can be used.
message = "Outside"
def show_message():
message = "Inside"
print(message)
show_message()
print(message)This prints Inside, then Outside. The two spellings of message do not automatically mean one variable. The inner name belongs to show_message(); the outer name belongs to the surrounding program.
Scope answers a practical question: where can Python find this name? A name created outside a function belongs to the surrounding program. A parameter or a name created in a function body belongs to that current function call.
Keep the two areas separate: OUTSIDE FUNCTION: message = "Outside". CALL show_message(): create and use the local message, then print it. END CALL: those local working names are no longer used. BACK OUTSIDE: print(message) uses the outside name.
def greet(name):
message = "Hello " + name
print(message)
greet("Ana")During this call, name refers to "Ana" and message refers to "Hello Ana". Concept 16 showed that an argument gives a value to a parameter; that parameter name is available inside this function call.
language = "Python"
def show_language():
print(language)
show_language()If Python does not find a name created locally in the function, it can read a matching name from the surrounding program. Here no local language exists, so the function reads the outside value.
color = "blue"
def change_color():
color = "green"
print(color)
change_color()
print(color)Inside the function, color is "green". Outside it is still "blue". Creating the local color does not automatically replace the outside one; while Python works inside the function, the local name hides the outside name with the same spelling.
def make_label():
label = "Local"
print(label)
make_label()
# print(label) would not find this local name outsidelabel works while Python is inside make_label(). After the call, outside code cannot simply expect print(label) to work, because Python cannot find that local name there.
def make_label():
label = "Python"
return label
course_label = make_label()
print(course_label)label is still local to make_label(). Concept 17 gives the useful bridge: return sends the value "Python" back, and outside code stores it in its own name, course_label. Think: local name → return value → outside name.
def greet(name):
message = "Hello " + name
print(message)
greet("Ana")
greet("Marko")In the first call, name is "Ana" and message is "Hello Ana". In the second, they are "Marko" and "Hello Marko". Do not imagine a parameter or local variable as one permanent global box.
A practical scope-reading checklist
- Locate yourself in the programAre you reading outside a function or inside the current function call?
- Find locally created namesWas this name created in the current function body?
- Check for a parameterIs the name a parameter of the current function call?
- Then look outside for a readable nameIf no local name exists, is there an outside name Python can read?
- Return a value that is needed outsideWhen outside code needs a useful value, return it instead of trying to reach for a local name.
Common beginner mistakes
- Assuming the same spelling means the same variableThe same name can belong to the surrounding program and to a function call. Check where each name was created.
- Expecting a local variable to exist outside the functionA name created in a function body is for that function call; outside code cannot automatically use it after the call ends.
- Thinking a local change automatically changes the outside nameAssigning a local name with the same spelling does not automatically replace the outside name.
- Forgetting that parameters belong to the function callAn argument supplies a value for a parameter during one call. Read that parameter as a name available inside that call.
- Trying to use a local name outside instead of returning a valueIf outside code needs a result, use
returnand store the returned value in an outside name. - Losing track of which scope a name belongs toPause at each use of a name and ask whether Python is currently working inside or outside the function.
