Are numeric types objects?
>>> isinstance(1, object)
True
Apparently they are. :-).
Note that you might need to adjust your mental model of an object
a little. It seems to me that you're thinking of object
as something that is "mutable" -- that isn't the case. In reality, we need to think of python names as a reference to an object. That object may hold references to other objects.
name = something
Here, the right hand side is evaluated -- All the names are resolved into objects and the result of the expression (an object) is referenced by "name".
Ok, now lets consider what happens when you pass something to a function.
def foo(x):
x = 2
z = 3
foo(z)
print(z)
What do we expect to happen here? Well, first we create the function foo
. Next, we create the object 3
and reference it by the name z
. After that, we look up the value that z
references and pass that value to foo
. Upon entering foo
, that value gets referenced by the (local) name x
. We then create the object 2 and reference it by the local name x
. Note, x
has nothing to do with the global z
-- They're independent references. Just because they were referencing the same object when you enter the function doesn't mean that they have to reference the function for all time. We can change what a name references at any point by using an assignment statement.
Note, your example with += may seem to complicate things, but you can think of a += 10
as a = a + 10
if it helps in this context. For more information on += check out: When is "i += x" different from "i = i + x" in Python?