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
#12
Nwb Wrote: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.
This criticism only applies to literal constants in the interactive interpreter. It is not the assignment that creates the copy.
>>> x = 8000
>>> y = x
>>> x is y
True
The reason for the behavior you mentioned above is that in the interactive mode, each statement is compiled separately because it must be executed immediately. So python compiles the statement Var = 5000, which creates a int object and executes it. When it compiles the second statement Var2 = 5000, it doesn't keep track of the previous constant 5000.

This is different if you compile a function (or a whole file), for example
>>> def func():
...     var = 5000
...     var2 = 5000
...     print(var is var2)
... 
>>> func()
True
This time the compiler can detect that the code uses the same constant twice because the statements in func's body are compiled together. All of this has nothing to do with the assignment operator.
>>> exec("""
... var = 5000
... var2 = 5000
... print(var is var2)
... """)
True
Also note that this is a very small issue, because the number of literal constants in code is usually small.
Reply
#13
Why are statements in a function compiled together? And what do you mean they are compiled together? Python doesn't just interpret line by line?
Reply
#14
Nwb Wrote:Python doesn't just interpret line by line?
No, python compiles code into a intermediary bytecode language. When this bytecode is loaded in memory, python creates "code object" instances, which are python objects. For every function that you define, there will be a code object. This code object contains the bytecode and various data such as the literal constants that are defined in the code. If there are literal integers 5000 in the function's body, python will create an integer instance stored in the code object and reuse the same instance no matter how many times you call the function.
Reply
#15
You can use the dis module to see the bytecode (if you are using CPython).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#16
someone here thinks the reference count increases when an assignment is made? well, OK, so. that is an internal detail. not that i would think anyone would do it this way, but there is a way to mange objects being referenced without using reference counts at all. does it matter? would you create your Python code different if that were so? i'm curious why internal details matter to you. are you planning to implement yet another Python engine? will you big giving it away for free?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#17
Skaperen Wrote:i'm curious why internal details matter to you.
Anyone who has written a few extension modules by using the CPython's C API is interested in internal details and in reference counts. I know that garbage collection in general doesn't need reference counts, there may exist implementations of python that don't use it. In any case, the assignment statement doesn't copy python objects. It only assigns a pointer value to an existing instance.
Reply
#18
(Apr-28-2019, 08:57 PM)Skaperen Wrote: someone here thinks the reference count increases when an assignment is made? well, OK, so. that is an internal detail. not that i would think anyone would do it this way, but there is a way to mange objects being referenced without using reference counts at all. does it matter? would you create your Python code different if that were so? i'm curious why internal details matter to you. are you planning to implement yet another Python engine? will you big giving it away for free?
You didn't answer the questions that I asked you in my previous post but anyways.
Who said anything about reference count? And yes of course the reference count increases when there is an assignment. Your language is alluding as though that's not true. But it is true.

"but there is a way to mange objects being referenced without using reference counts at all."
What is the other way?

And finally to your question, which I had already answered in my previous post by the way, I'm trying to understand the "why" Python chose this design.

Your argument that you should not care about how things work, in my opinion, is lazy. If you don't understand how a language translates your code behind the scenes, you are bound to run into complications. Yet however I would stress myself that these are abstractions most programmers do not worry about.

But this is about how values are handled in Python at the highest level. What is wrong with knowing how they work? And it's not that there is no benefit in knowing ho. I also would definitely want to use C extensions in the future.

Griboullis that's cool. Can you recommend any material for details like this which happen behind the scenes in Python. How did you personally learn all of this?
Reply
#19
i'm not going to get into a discussion of object management techniques in system design. it's a much too complex subject and would take too much of my time. if you do want to add C-level extensions to a particular implementation, then, of course, you need to know the details of how that is done. i was originally focusing on programming in Python, itself.

BTW, i am curious enough about these C-level extensions to hope that some of you will make some free and open to the public.
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