Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Intricacies of Aliasing
#3
All of these are Python peculiar properties:
Small integers are preallocated in array called small_ints see CPython implementation
It covers interval from -5 to 256.
So, if x=500 and y=500, id(x) == id(y) will return False.
Internal behavior with string is harder to explain. Python preallocates each Python string that contains only ascii-chars and digits (may be some special chars too, i didn't explore this in deep) (see here). So, if you try to do the same with, e.g. abc-1, you will end with False:
x = 'abc-1'
y = 'abc-1'
id(x) == id(y)
# False
This is because these strings contains -.
Finally, each time you are creating a list, a new object is created, even if you try to create an empty list:
x = []
y = []
id(x) == id(y)
#False
Since, -5 to 256 numbers are prealloced objects, you can do some terrible things (using ctypes which allows to get direct access to memory), e.g. let assign to object 5 different value, e.g. 8:
import ctypes
mnt = id(6) - id(5) # the number of bytes per num
ctypes.memmove(id(5), id(8), mnt)  # Try to break Python's mind.... (Don't do this)
5 + 3
Output:
11
Reply


Messages In This Thread
Intricacies of Aliasing - by ClassicalSoul - Mar-01-2019, 11:57 AM
RE: Intricacies of Aliasing - by snippsat - Mar-01-2019, 12:58 PM
RE: Intricacies of Aliasing - by scidam - Mar-01-2019, 01:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  signal anti-aliasing frohr 0 1,196 May-23-2022, 05:18 PM
Last Post: frohr

Forum Jump:

User Panel Messages

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