Live data from Hacker News

Weird Python Integers

kate.io

31–40 of 150 posts

Re: Weird Python Integers

#31
I remember implementing this too on Nova 1200. When the address space is bigger than the memory, you can place those integers outside the memory. Those objects do not actually exist in other words. Saves you memory cycles too, because you can calculate the numeric value from the address.

Re: Weird Python Integers

#32
post #24

https://github.com/adtac/exterminate Plugging my near useless Python library that does this and a lot of other subtle, annoying things to break programs. The library is essentially a display of how much Python actually exposes to the user and how modifiable it is.

Why does my inner self find it immensely fun to modify programs in subtle ways to watch how they break? Considering there's infinite ways to write junk code, and far fewer ways to make it work.

Re: Weird Python Integers

#33
post #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`. ED…

It never ceases to amaze me that Python seems to be the only one that got this right - namely, that == should have value comparison semantics, for all types, and that some other (preferably distinctive enough, rather than something like ===) operator should always compare references.

Then you look at something like C#, where == compares values for value types and references for reference types, except that == is overloaded for some "value-like" reference types like String... it's a mess.

OTOH, Java is consistent in a sense that == never dereferences, but awkward in practice because of the need to use equals() for strings, which is too verbose for an operation that's far more common than object identity comparison.

Re: Weird Python Integers

#35
post #24

https://github.com/adtac/exterminate Plugging my near useless Python library that does this and a lot of other subtle, annoying things to break programs. The library is essentially a display of how much Python actually exposes to the user and how modifiable it is.

Wow, the alternative print is extra evil ...

    >>> from exterminate import AltPrint
    >>> print("Hey! How are you?")
    Yo dawwwwg! Wuz crackalackin' yo?
... because that string is not calculated locally, but retrieved via HTTP from an API call. So everything you print is sent to an external server.

AltPrint calls Gizoogle.translate():

https://github.com/adtac/exterminate/blob/f18862ed5b2e143e18...

And Gizoogle.translate() performs an HTTP call:

https://github.com/adtac/exterminate/blob/f18862ed5b2e143e18...

Re: Weird Python Integers

#36

    In [7]: a = "foo"

    In [8]: b = "foo"

    In [9]: a is b
    Out[9]: True

    In [10]: b = "foobaljlajdfsklfjds l;kjsl;dfj ls;dfj l;skdj flsdjluejsklnm "

    In [11]: a = "foobaljlajdfsklfjds l;kjsl;dfj ls;dfj l;skdj flsdjluejsklnm "

    In [12]: a is b
    Out[12]: False
Seems to work with small and big strings too.

Re: Weird Python Integers

#40

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.

Micropython does a much better job, using pointer alignment guarantees to pack in small integers:

https://micropython.org/

Apparently it is too hard to move python proper over to that method because of backwards compatibility issues with C extensions.

Post reply on HN