Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
intering int number
#1
hi
in the below code:
#from: https://realpython.com/python-assignment-operator/
''' to detemine intering range for integers'''

from platform import python_version

interning = [
    x
    for x, y in zip(range(-10, 500), range(-10, 500))
    if x is y
]

print(
    f"Interning interval for Python {python_version()} is:"
    f" [{interning[0]} to {interning[-1]}]"
)

x=-4
y=-4
print(id(x)==id(y))    #True
x=257
y=257
print(id(x)==id(y))   #in shell is  false but in script run results true ?
x=256
y=256
print(id(x)==id(y))    #True
after running( i use Thonny) is:
Output:
Interning interval for Python 3.10.11 is: [-5 to 256] True True True
my issue is about line 22.
in shell if i write:
>>> x=257
>>> y=257
>>> id(x)==id(y)
False

but this is inconsistence with result of running line 22 of the above code.
can explain to me the reason of this?
thanks for any reply.
Reply
#2
(Apr-28-2024, 07:42 AM)akbarza Wrote: but this is inconsistence with result of running line 22 of the above code.
can explain to me the reason of this?
It can be explained very easily by considering the bytecode generated by Python. Consider the following example
>>> def func():
...     x = 257
...     y = 257
...     return x is y
... 
>>> func()
True
>>> import dis
>>> dis.dis(func)
  2           0 LOAD_CONST               1 (257)
              2 STORE_FAST               0 (x)

  3           4 LOAD_CONST               1 (257)
              6 STORE_FAST               1 (y)

  4           8 LOAD_FAST                0 (x)
             10 LOAD_FAST                1 (y)
             12 IS_OP                    0
             14 RETURN_VALUE
>>> 
In this code, the assignements x = 257 and y = 257 occur in the body of the same function. When Python compiles this into bytecode, it allocates a single space in memory to store the literal constant 257 which appears twice in the body of the function. When executing the assignment statements, it simply points the variables x and y to the position where this memory was allocated. Hence x and y have the same memory address and x is y returns True.

On the other hand, if you run interactively
>>> x = 257
>>> y = 257
then every statement is compiled separately into bytecode when you hit the return key. Each time, Python will allocate a new space in memory to store the constant 257, hence x and y will point to two different memory addresses.

The result of the compilation of Python code is a «code object». The code object contains the bytecode and also the literal constants that appear in the code. When your script is compiled, a single code object is created, hence several occurrences of the same constant can be shared. On the other hand, when two statements are executed interactively, two code objects are created and they cannot share their constants.
Larz60+ and akbarza like this post
« We can solve any problem by introducing an extra level of indirection »
Reply


Forum Jump:

User Panel Messages

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