coderboy
coderboy

Reputation: 1859

Are Python integers reference types?

We all know that lists in Python are reference types i.e a variable holding a list doesn't hold the list directly, but rather holds a reference to it.

And we all know even the fact that everything in Python is an object.

So the question that comes in my mind is that integers are also objects in Python and so variables holding integers would also contain references to the integers; then why are only lists classified as reference types?

Aren't integers in Python also reference types following from the fact that everything is an object in Python?

Upvotes: 1

Views: 1428

Answers (1)

Weeble
Weeble

Reputation: 17920

These concepts don't really apply in Python. In my experience "reference type" and "value type" are mostly used in .NET languages such as C#, where they have specific meanings - variables of a reference type store a reference to an object, while variables of a value type store the value of the object directly. You can't obtain a reference to that object stored in the variable (although you may be able to create a "boxed" copy of that value and obtain references to the box). The terms may be used by analogy in other languages - but for example in Java all classes would be what .NET considers "reference types" and only primitives behave as "value types" do in .NET, so there the terms aren't really as necessary. So let's return to Python.

If we use the .NET meaning, then yes, all Python types are effectively "reference" types, in that every variable is a reference* to an object and doing a = b will make a refer to the same object as b. However, the thing about "reference types" and "value types" is that when an object is immutable it no longer really matters whether it's of a reference type or a value type. You can't do the one big thing where the distinction matters - you can't have two references to the same object, modify the object through one of them and then be surprised when the other changes!

This is a long-winded way to say that when people call integers "value types" in Python they are probably thinking about them in terms of C# or maybe Java, and observing that they behave much the same as value types do in C# or primitives do in Java, because they do! But that's really because they're immutable more than anything else.


* Here I'm using "reference" in a more general sense than the specific one found in C#. The general sense is of some kind of handle, address or identifier that specifies a particular object, such that we can have many of these "references" to a single object. Both "reference" and "pointer" are general terms that may have more specific meanings in certain languages. For example a C# reference is a very similar concept to a Java reference, but rather different from a C++ reference. In some languages a "pointer" is specifically a numeric memory address that you can manipulate with arithmetic, in other languages it is indistinguishable from the general sense I described for "reference". I find the term "reference" to be the most neutral and most useful to describe what we have in Python, but be careful with all these terms when comparing one language to another.

Upvotes: 4

Related Questions