Understanding Python’s call-by-object style of passing function arguments

The key difference is that in C-style language, a variable is a box in memory in which you put stuff. In Python, a variable is a name.

Python is neither call-by-reference nor call-by-value. It’s something much more sensible! (In fact, I learned Python before I learned the more common languages, so call-by-value and call-by-reference seem very strange to me.)

In Python, there are things and there are names. Lists, integers, strings, and custom objects are all things. x, y, and z are names. Writing

x = []

means “construct a new thing [] and give it the name x“. Writing

x = []
foo = lambda x: x.append(None)
foo(x)

means “construct a new thing [] with name x, construct a new function (which is another thing) with name foo, and call foo on the thing with name x“. Now foo just appends None to whatever it received, so this reduces to “append None to the the empty list”. Writing

x = 0
def foo(x):
    x += 1
foo(x)

means “construct a new thing 0 with name x, construct a new function foo, and call foo on x“. Inside foo, the assignment just says “rename x to 1 plus what it used to be”, but that doesn’t change the thing 0.

Leave a Comment