Python Forum
About integer objects vs integer values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
About integer objects vs integer values
#1
Would it be more efficient to have integers represented by objects or values?
Eg if it's represented by an object,
Var = 5
Var points to an object which has the value '5'
Var = 5
Var is a location in memory representing '5'

When Var is reassigned, in object representation, a new object is created whereas if it was the value case then the memory location would just changed what value it stores

Why does Python use object representation for integers and strings)?
Is memory address value representation faster than object representation?
Reply
#2
Nwb Wrote:Why does Python use object representation for integers and strings)?
Python is a high level language. By design, every piece of data that python manipulates is an instance of some python class. In CPython, it means that it is implemented as a C struct type with at least a few fields the size of a C pointer. In modern python, there is no limit to the size of integers, which means that an integer value may take a variable size in memory.
Nwb Wrote:When Var is reassigned, in object representation, a new object is created
This is not true if you assign a constant value such as 5. The constant integer instance is created in the compiled code object, and the same instance is used with an increased reference count.

There are several ways to work closer to memory, you can use numpy to manipulate arrays of C type integers or floating types in memory, you can also use languages such as cython that allow you to include pieces of C code in the python code.
Reply
#3
integers are already objects. they just happen to be very simple and perhaps implement more directly. there are also literals that are integers. they can be or can make an integer object, as needed.

object representation of things we might not ordinarily think of as objects helps us work with these things in a more uniform way.

are you wanting to code more in C?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
From Documentation » Python/C API Reference Manual » Concrete Objects Layer » Integer Objects

Quote:The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object.

If you are interested in low-level C, then this stackoverflow post may help.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
rumor is that ints, floats, and complexes are immutable. my guess is that bools are, too.

but does it really matter as long as the behavior is as if it is a referenceable object?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
(Apr-27-2019, 12:22 AM)Skaperen Wrote: but does it really matter as long as the behavior is as if it is a referenceable object?

In case your code somehow depend on object identity then it will matter:

>>> x = 5
>>> y = 5
>>> id(x) == id(y)       # reference to the same object because in range -5..256
True
>>> z = 1000
>>> n = 1000
>>> id(z) == id(n)       # reference to different objects because not in range -5..256
False
>>> a = 1001
>>> b = a
>>> id(a) == id(b)
True
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
if it quacks like an int ...
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
Quote:rumor is that ints, floats, and complexes are immutable. my guess is that bools are, too.

but does it really matter as long as the behavior is as if it is a referenceable object?

Yes they're all immutables, including bool. You can check by seeing whether the id of an object of the built-in class changes when you assign to it.

Of course it is very important to know whether the type is immutable. Because when you reassign to an int for example, you are pointing to an entirely new object.

That means that if you pass an integer to a function, you cannot have the function affect the value of the integer passed in without having the function return a value and then further using this return value.

However you can't say the same thing with mutables.

Quote:are you wanting to code more in C?
Why did you say that?

Anyways back to the thread topic.

I asked why Python chose to represent integers with objects. I'm just curious.

In C, assigning to an integer variable would mean changing value at the SAME memory location.

But in Python, assigning to an integer variable would mean allocating new memory for a new object, constructing integer object with particular value, pointing to new location, destructing old object. Which obviously takes much longer time than just changing bits at a memory location.

So why did Python choose to have this design?
Reply
#9
Nwb Wrote:But in Python, assigning to an integer variable would mean allocating new memory for a new object
This is not true. Assigning to an integer variable simply increases the reference count of an existing (python) integer instance.

Your reasoning works for certain operations such as augmented assignments
x += 1
In python this augmented assignment creates a new PyInt_Object because the intitial object may have other references pointing at it. Variables, better called names in python, are only references to shared objects.

The design of python is excellent, which explains the success of the language. The best thing to do if you want to understand it is to write a few C extension modules using the C api.
Reply
#10
back to the original questions.

(Apr-25-2019, 04:55 AM)Nwb Wrote: Would it be more efficient to have integers represented by objects or values?
Eg if it's represented by an object,
Var = 5
Var points to an object which has the value '5'
Var = 5
Var is a location in memory representing '5'

When Var is reassigned, in object representation, a new object is created whereas if it was the value case then the memory location would just changed what value it stores

Why does Python use object representation for integers and strings)?
Is memory address value representation faster than object representation?

these questions are about implementation of Python. it could be implemented either way. and which way is best can depend on the host machine architecture.

is it an object or a value. is it a particle or a wave?

what matters to Python programming is the behavior, not the implementation. different implementations may do things in different ways to achieve the same behavior. if the behavior is correct then that implementation is usable to run valid Python code.

i can even envision a purely hardware implementation. would it matter then? i'd like to see the OS on such a machine.

try writing some Python code that uses normal Python code (not special implementation features) to determine of the implementation it is being run by uses objects or values for each of these types: bool, int, float, complex.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Does the integer implementation matter? ichabod801 2 793 Oct-22-2019, 03:26 PM
Last Post: snippsat
  integer bases Skaperen 7 2,569 Nov-24-2017, 09:46 AM
Last Post: heiner55
  multiplying integer to decimal ArnabRoyBatman 8 7,136 Jun-20-2017, 04:49 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020