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
#11
(Apr-27-2019, 04:31 PM)Gribouillis Wrote:
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.

"Assigning to an integer variable simply increases the reference count of an existing (python) integer instance."
That is only for -5 to 255 right. But if you assign an integer variable a value that isn't part of that range, then it creates a new object.

Python also doesn't point to the same integer object though it already exists
Var = 5000
Var2 = 5000

Var and Var2 are pointing to different objects according to the interpreter.

I just wanted to know the advantages and disadvantages of the design. I'm not questioning it's success.

I don't know why Python chose to do it this way among other things, I want to know.
It may be better, but just saying that it's better doesn't give any information to me Huh

Quote: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.
Sorry I didn't follow. Why would it depend on the host machine whether objects or values would be more efficient?

Quote: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 get that but I'm just curious about why Python chose this design. Because wouldn't using values be just much faster? What are the advantages/disadvantages then? That's what I want to know.

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

Quote: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.
You say "not special implementation features". Are there other good implementations for CPython?
And what do you mean determine whether it uses objects or values? Everything in Python uses objects.
Reply


Messages In This Thread
About integer objects vs integer values - by Nwb - Apr-25-2019, 04:55 AM
RE: About integer objects vs integer values - by Nwb - Apr-28-2019, 04:02 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Does the integer implementation matter? ichabod801 2 1,846 Oct-22-2019, 03:26 PM
Last Post: snippsat
  integer bases Skaperen 7 4,737 Nov-24-2017, 09:46 AM
Last Post: heiner55
  multiplying integer to decimal ArnabRoyBatman 8 12,704 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