3 ms·
"If I call a function f(a, b), I know that a, b are still pointing at the same object in e.g. Python." def f(x): global a a = "No, you don't" a =
by Someone 10y ago
"If I call a function f(a, b), I know that a, b are still pointing at the same object in e.g. Python."
def f(x):
global a
a = "No, you don't"
a = "I know"
f(a)
print a
This counterexample may be far-sought, but it is a minimal version of what can easily happen in larger programs that have shared mutable state.
But yes, call by reference can be confusing. It also can be very useful, though. That's why many modern languages have call by reference, but also require callers to specify it at each call site. I think that gives you the best of both worlds.