Python Forum
About integer objects vs integer values - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: About integer objects vs integer values (/thread-17815.html)

Pages: 1 2


About integer objects vs integer values - Nwb - Apr-25-2019

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?


RE: About integer objects vs integer values - Gribouillis - Apr-25-2019

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.


RE: About integer objects vs integer values - Skaperen - Apr-26-2019

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?


RE: About integer objects vs integer values - perfringo - Apr-26-2019

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.


RE: About integer objects vs integer values - Skaperen - Apr-27-2019

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?


RE: About integer objects vs integer values - perfringo - Apr-27-2019

(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



RE: About integer objects vs integer values - Skaperen - Apr-27-2019

if it quacks like an int ...


RE: About integer objects vs integer values - Nwb - Apr-27-2019

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?


RE: About integer objects vs integer values - Gribouillis - Apr-27-2019

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.


RE: About integer objects vs integer values - Skaperen - Apr-28-2019

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.