Live data from Hacker News

Weird Python Integers

kate.io

1–10 of 150 posts

Re: Weird Python Integers

#2
Summary: Integers in python are full blown objects. Small numbers are stored in a central preallocated table where each entry represents one number. Setting a variable to a small integer makes it point to an entry in that table. Multiple variables may point to the same small integer objects in that table. Fooling around with the table leads to funny results.

Re: Weird Python Integers

#5
the python documentation [1] says the following:

> The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object. So it should be possible to change the value of 1. I suspect the behaviour of Python in this case is undefined. :-)

does anyone have any idea how they chose that range? it's a 262-wide block starting at -5, which seems incredibly arbitrary.

[1] https://docs.python.org/2/c-api/int.html

Re: Weird Python Integers

#6
Lisp systems also have fixnums and bignums. For example a 64bit Common Lisp:

  MOST-NEGATIVE-FIXNUM, value: -1152921504606846976                                      
  MOST-POSITIVE-FIXNUM, value: 1152921504606846975        
Fixnums are typically stored inline in data structures (like lists, arrays and CLOS objects). Bignums will be stored as a pointer to an heap-allocated large number. Data has tags and thus in a 64bit Lisp the fixnums will be slightly smaller than 64bit. Bignums can be 'arbitrary' larger and there is automatic switching between fixnums and bignums for numeric operations.

Re: Weird Python Integers

#7

the python documentation [1] says the following: > The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object. So it should be possible to change the value of 1. I suspect the behaviour of Python in this case is undefined. :-) does anyone have any idea how they chose that range? it…

You could probably find out from the code's log. I'd guess the small positive integers are common due to e.g. iteration or len() of small collections and the like, and the very small negatives are due to things like error values.

Re: Weird Python Integers

#9
> That is suprising! It turns out that all “small integers” with the same value point to the same memory. We can use the Python built-in function id which returns a value you can think of as a memory address to investigate.

Unfortunately this blog post seems to miss a great opportunity to show you how you should compare integers for equality -- using the equality operator `==` and not the identity comparison `is`.

EDIT: odd, this post attracted a lot of downvotes. Please help me learn how this post could be improved.

Re: Weird Python Integers

#10
post #7

the python documentation [1] says the following: > The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object. So it should be possible to change the value of 1. I suspect the behaviour of Python in this case is undefined. :-) does anyone have any idea how they chose that range? it…

You could probably find out from the code's log. I'd guess the small positive integers are common due to e.g. iteration or len() of small collections and the like, and the very small negatives are due to things like error values.

The small negatives might also be for indexing from the end of lists. `my_list[-1]` is super common.
Post reply on HN