0
0
Pythonprogramming~10 mins

Procedural vs object-oriented approach in Python - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Procedural vs object-oriented approach
Start
Choose Approach
Procedural
Write functions
Object-Oriented
Define class
This flow shows two ways to organize code: procedural uses functions step-by-step, object-oriented uses classes and objects with methods.
Execution Sample
Python
def add_proc(x, y):
    return x + y

class Adder:
    def add(self, x, y):
        return x + y

result_proc = add_proc(2, 3)
adder = Adder()
result_obj = adder.add(2, 3)
print(result_proc)
print(result_obj)
This code shows adding two numbers using a procedural function and an object-oriented class method.
Execution Table
StepActionEvaluationResult
1Define function add_procStore function in memoryFunction ready to use
2Define class Adder with method addStore class and methodClass ready to create objects
3Call add_proc(2, 3)Add 2 + 35
4Create object adder = Adder()Allocate objectObject adder created
5Call adder.add(2, 3)Call method add with 2,35
6Print resultsOutput values5 and 5 printed
💡 All steps executed, program ends after printing results
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
add_procundefinedfunction storedfunction storedfunction storedfunction stored
Adderundefinedclass storedclass storedclass storedclass stored
adderundefinedundefinedobject createdobject createdobject created
result_procundefined5555
result_objundefinedundefinedundefined55
Key Moments - 3 Insights
Why do we need to create an object to use the add method in the object-oriented approach?
Because the add method is inside the class and works on objects, so we must create an object (see execution_table step 4 and 5) to call the method.
Can we call the procedural function without creating anything?
Yes, the procedural function is standalone and can be called directly (see execution_table step 3).
Are the results from both approaches the same?
Yes, both return 5 when adding 2 and 3 (see execution_table steps 3 and 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of calling add_proc(2, 3)?
A2
B3
C5
DError
💡 Hint
Check execution_table row 3 under Result column
At which step is the object 'adder' created?
AStep 4
BStep 3
CStep 5
DStep 2
💡 Hint
Look at execution_table row 4 Action column
If we remove the class and only keep the procedural function, what changes in variable_tracker?
A'result_obj' will be 5
B'adder' variable will be undefined throughout
C'add_proc' will be undefined
D'Adder' will be an object
💡 Hint
See variable_tracker row for 'adder' which is created only after Step 4
Concept Snapshot
Procedural approach:
- Write functions
- Call functions directly
- Simple and linear

Object-oriented approach:
- Define classes
- Create objects
- Call methods on objects

Both can produce same results but organize code differently.
Full Transcript
This lesson compares procedural and object-oriented programming. Procedural uses functions you call directly. Object-oriented uses classes to create objects, then calls methods on those objects. The example adds two numbers both ways. The execution table shows defining functions and classes, calling them, and printing results. Variables track function, class, and object states. Key moments clarify why objects are needed for methods and that procedural functions are standalone. The quiz checks understanding of steps and variable states. The snapshot summarizes the main differences simply.