What Really Happens Inside Python When You Call a Function?
This article explains step by step how Python creates a function object, builds a call stack, handles parameters, executes the body, performs garbage collection, and manages recursion, illustrating each stage with clear code examples and diagrams.
Today we explore an interesting topic: what actually happens inside Python when a function is called.
Consider a very simple function:
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))When we define and call this function, Python performs many internal operations to produce the expected result.
Step 1: Create a Function Object
In Python, defining a function creates a function object that is stored in memory like any other variable.
def greet(name):
return f"Hello, {name}!"
print(type(greet))The object greet has a name and a reference in memory. You can inspect it with:
print(greet.__name__)
print(greet)Step 2: Create an Execution Stack
When greet("Alice") is called, Python creates a new stack frame on top of the call stack.
The main stack frame is where the program runs. Each function call pushes a new frame; when the function returns, the frame is popped and execution resumes in the previous frame.
Example:
def bar():
print("Inside bar()")
def foo():
print("Inside foo(), calling bar()")
bar()
print("Returned to foo() after bar() finished")
def main():
print("Inside main(), calling foo()")
foo()
print("Returned to main() after foo() finished")
print("Inside main(), calling bar()")
bar()
print("Returned to main() after bar() finished")
main()The following diagram visualizes the stack changes:
Program starts with main(). main() calls foo(), creating a stack frame for foo().
Inside foo(), bar() is called, adding a frame for bar().
When bar() finishes, its frame is removed and control returns to foo().
After foo() finishes, its frame is removed and execution returns to main(). main() calls bar() again, adding another frame.
When this bar() finishes, only the main() frame remains.
Step 3: Parameter Handling
When arguments are passed, Python either passes a reference to the object (for mutable types like lists or dictionaries) or passes a copy of the reference (for immutable types like integers and strings).
Mutable example:
def modify_list(lst):
lst.append(100)
my_list = [1, 2, 3]
modify_list(my_list)
print(my_list)Immutable example:
def modify_number(num):
num += 10
x = 5
modify_number(x)
print(x)Because integers are immutable, num += 10 creates a new integer object rather than modifying the original.
Step 4: Execute the Function Body
During execution, parameters are assigned to local variables and the function body runs line by line until a return statement provides the result.
Example:
def calculate(a, b):
result = a + b
return result
x = calculate(3, 7)Here, a receives 3, b receives 7, the sum is computed, and 10 is returned and stored in x.
Step 5: Garbage Collection
After a function finishes, its local variables are cleared from memory by Python’s automatic garbage collector.
def example():
x = 100
example()
print(x) # NameError because x is not defined outside the functionIf an object is still referenced (e.g., returned from the function), it will not be collected.
def outer():
x = [1, 2, 3]
return x
y = outer()
print(y)Recursion
Recursive functions call themselves until a base condition is met, adding a new stack frame for each call. Excessive recursion can cause a stack overflow.
def countdown(n):
if n == 0:
print("Done")
return
print(n)
countdown(n - 1)
countdown(5)An infinite recursion example:
def infinite():
return infinite()
infinite() # RecursionError: maximum recursion depth exceededYou can check Python’s default recursion limit (usually 1000) with:
import sys
print(sys.getrecursionlimit())Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Code Mala Tang
Read source code together, write articles together, and enjoy spicy hot pot together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
