Why are Python integers implemented as objects?
Asked Answered
C

1

2

Why are Python integers implemented as objects?

The article Why Python is Slow: Looking Under the Hood as well as its comments contain useful information about the Python memory model and its ramifications, in particular wrt to performance.

But this article does not ask or answer the question why the decision to implement integers as objects was made in the first place.

In particular, referring to Python as dynamically typed is not an answer. It is possible to implement integers as integers in a dynamically typed language.

Chauchaucer answered 6/6, 2020 at 19:3 Comment(14)
I don't think this is the answer you're looking for, but Python gained some flexibility when deciding to have everything as an object. For example, you can subclass int (look at bool).Extraversion
Does this answer your question? "is" operator behaves unexpectedly with integersSukkah
No, my question is not about the "is" operator ... I see that I am misleading people with the comment about "is" ... I deleted this comment now. Apologies for creating any misunderstandings.Chauchaucer
@MarioIshac Thanks for your comment about subtyping. But I would doubt that it could justify such an important design decision.Chauchaucer
Speed is not the overriding goal of the design of either the Python language or the CPython implementation.Ochrea
#866411Sukkah
Apart from disadvantages concerning speed, the memory model of objects is more complicated than a memory model of locations and values. While I understand that one wants objects, I think integers-as-objects makes integers more difficult to understand than necessary. This only makes my question more ugent.Chauchaucer
@Sukkah Thanks for the link to an interesting discussion. But this does not answer my question as far as I can see.Chauchaucer
Did you read #866411 ?Sukkah
Objects are more complex than primitives, but having both objects and primitives is more complex and much harder to work with than having only one or the other.Ochrea
Thanks, Joe, the comment you link to contains a (broken) link to GvR's blog python-history.blogspot.com/2009/02/first-class-everything.html Coming from GvR, this is valuable. But to say "One of my goals for Python was to make it so that all objects were first class" is begging the question. Why is that a good goal to pursue? One consequence of pursuing this goal is that one needs to declare some objects as immutable. This seems to run against the very idea of an object. So I would like to learn a bit more about the background considerations that lead to such design decisions.Chauchaucer
" One consequence of pursuing this goal is that one needs to declare some objects as immutable. This seems to run against the very idea of an object." - you've got some weird ideas about what objects are for.Ochrea
I object to being called out as having "weird ideas". I am politely asking why, when designing a programming language, one would go for a design in which one has immutable and mutable objects, if it was also possible to simply have all objects mutable, if one decided to also have primitive data types.Chauchaucer
"both objects and primitives is more complex and much harder to work with than having only one or the other" --- any evidence or examples for this?Chauchaucer
R
0

"Python being a dynamically typed language" is one of the answers. Another thing to know: python's int is not the same integer you will get in most other languages. Python int actually supports long arithmetics (i.e. can hold values bigger than 2^64). It is slower which is unavoidable yet it gives you some additional flexibility.

As for the "weird" behavior: don't use is in python in order to compare values. What python is does is checks, that two variables point to the exact same object. It does so by comparing their ids. This is not what you want most of the time. As the rule of thumb - use is only to check for is None and nothing else.

Rebozo answered 6/6, 2020 at 19:33 Comment(2)
Thanks, but all you are saying is already explained in the linked article. My question was why the decision of implementing integers as objects was made in the first place.Chauchaucer
I removed the afterthought about "is" from my question to make it more focused.Chauchaucer

© 2022 - 2024 — McMap. All rights reserved.