Weird Python Integers
31–40 of 150 posts
Re: Weird Python Integers
#32https://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.
Re: Weird Python Integers
#33> 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…
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
#34Re: Weird Python Integers
#35https://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.
>>> 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
#37Equal Rights for Functional Objects or, The More Things Change, The More They Are the Same (1993) by Henry Baker
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.23.9...
Re: Weird Python Integers
#38Yup, 0day hunters have fun with this behavior from time to time.
Re: Weird Python Integers
#39Re: Weird Python Integers
#40Summary: 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.
Apparently it is too hard to move python proper over to that method because of backwards compatibility issues with C extensions.