15

I am aware that numeric values are immutable in python. I have also read how everything is an object in python. I just want to know if numeric types are also objects in python. Because if they are objects, then the variables are actually reference variables right? Does it mean that if I pass a number to a function and modify it inside a function, then two number objects with two references are created? Is there a concept of primitive data types in python?

Note: I too was thinking it as objects. But visualizing in python tutor says differnt: http://www.pythontutor.com/visualize.html#mode=edit

def test(a):
    a+=10
b=100
test(b)

Or is it a defect in the visualization tool?

codingsplash
  • 4,785
  • 12
  • 51
  • 90

2 Answers2

26

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?

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • what about the second part of the question? – farhawa May 20 '15 at 04:59
  • @WajdiFarhani -- Sorry, maybe a little too anxious to post. I've elaborated quite a bit hopefully covering the full answer. – mgilson May 20 '15 at 05:08
  • @mgilson Thanks for the link! On a side note can you let me know why the visualization is different? why are the numbers not represented as objects? – codingsplash May 20 '15 at 05:15
  • 1
    @mgilson you have mentioned " we look up the value that z references and pass that value to foo"...are you saying numbers are pass by value and not pass by reference? – codingsplash May 20 '15 at 05:17
  • @user567 -- I've heard it described as "pass by object" -- Basically, you unpack the reference into it's object and then you create a new reference to it in the function. Fundamentally I don't really see much difference to passing a pointer in C. You can modify the thing that the pointer points to in the function, but you can't make the pointer point to something else (and expect to see that change outside the function). The only difference is that some python objects don't expose any ways to modify them ("immutable"). – mgilson May 20 '15 at 05:21
14

Everything in Python is an object, and that includes the numbers. There are no "primitive" types, only built-in types.

Numbers, however, are immutable. When you perform an operation with a number, you are creating a new number object.

nneonneo
  • 171,345
  • 36
  • 312
  • 383