I wrote the following code to check if integers are passed by value or reference.
foo = 1
def f(bar):
print id(foo) == id(bar)
bar += 1
print foo, bar
f(foo)
The output I get is
True
1, 2
From the Python documentation, id(object)
returns the identity of an object. In the CPython implementation, this is the address of the object in memory. Since the first statement in the function body returns True
, it means foo
has been passed by reference, but why does the last statement print 1, 2
instead of 2, 2
?