Pass by Value
- The function receives a copy of the argument’s value.
- Modifications to the parameter inside the function do not affect the original argument.
Pass by Reference
- The function receives a reference (or address) to the actual argument.
- Modifications to the parameter inside the function affect the original argument because both the argument and the parameter refer to the same memory location.
Mutable Objects (like lists or dictionaries):
The function can modify the object in place, and those changes will be reflected outside the function.
my_list
is a list, which is mutable.- When
my_list
is passed tomodify_list()
, the function receives a reference to the same list object. - The
append
operation modifies the list object directly, somy_list
reflects this change outside.

Immutable Objects (like integers, strings, tuples):
Since these objects cannot be modified, any operation that seems to change the object actually creates a new object. This makes it appear like “pass by value,” but technically it’s still passing a reference to the original object.
my_number
is an integer, which is immutable.- When
my_number
is passed tomodify_integer()
, the function gets a reference to the integer object10
. - Inside the function,
n += 1
creates a new integer object11
and assigns it to the local variablen
, but this does not affectmy_number
outside the function.
Key Takeaways
- Python is “pass by object reference”: It passes references to objects, but the way these references behave depends on whether the object is mutable or immutable.
- Mutable objects (like lists and dictionaries) can be changed in place inside a function, and these changes will reflect outside the function.
- Immutable objects (like integers, strings, and tuples) cannot be altered in place; operations that seem to modify them actually create new objects.
In Python, mutable objects are passed by reference (or more accurately, by object reference).
note
-
Mutable ဆိုတာက object creation လုပ်ပီးပေမဲ့ change လို့ရတာ
-
Pass by reference ကလဲ value passပေးလိုက်ပေမဲ့ object ရဲ့ memory location ပါpassပေးလိုက်တာ
-
တခုခု modify လုပ်လိုက်တာနဲ့ Original argument ကိုaffectဖြစ်စေတယ် အဲ့တော့ function အပြင်ရော အထဲမှာပါ valueရဲ့changesက affectဖြစ်သွားတယ်
-
Immutable object ကpass by valueနဲ့သဘောတရားတူ