Trick Passing An Immutable by Reference
Trick Passing An Immutable by Reference
In Python, we cannot explicitly pass an int (or any other immutable type)
by reference because integers are immutable. However, we can achieve similar
functionality by using mutable types, such as lists or custom objects, which can
store the integer value and thus be modified within a function.
modify_value(value)
print("After:", value[0]) # Output: After: 10
While we cannot explicitly pass an immutable type like an int by reference in
Python, we can use a mutable container, such as a list or a custom object, to
achieve similar behavior. This allows us to modify the value within a function
and see those changes reflected outside the function.