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


Messages In This Thread
intering int number - by akbarza - Apr-28-2024, 07:42 AM
RE: intering int number - by Gribouillis - Apr-28-2024, 08:55 AM

Forum Jump:

User Panel Messages

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