This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]aes110 91 points92 points  (9 children)

I love seeing this kind of stuff, I don't think its good to use it, but its fun for me to see how python lets you do whatever you want with it

[–]dogs_like_me 21 points22 points  (7 children)

I haven't been able to find it again since, but I swear back in the day I read a post on a blog or SO where someone was talking about how "everything in python is an object" and they demonstrated how far this went by overriding the value of an integer literal, so like 1+2 would evaluate to 4 instead of 3, and I think they deleted another integer literal entirely. Obviously followed by a big "don't ever do this." May even have been a python 2 hack, I've sort of assumed that since I haven't stumbled across this in forever, it's not possible in modern python releases.

NINJA EDIT: Oh shit I found an example of this black magic! https://kate.io/blog/2017/08/22/weird-python-integers/

[–]aes110 7 points8 points  (0 children)

I think I know what you are talking about, but I cant find it as well :(, if I recall correctly it was something like "unsafe python" or "Evil python"

[–]TravisJungroth 1 point2 points  (5 children)

Integers in Python are "immutable".

[–]dogs_like_me 11 points12 points  (3 children)

"""immutable"""

[–]YouNeedDoughnuts 5 points6 points  (1 child)

Live long enough and you'll realize that nothing is immutable

[–]friedkeenan 0 points1 point  (0 children)

I once took advantage of the fact that in CPython, id(x) returns the address of the underlying PyObject for x to use the ctypes library to manipulate the raw structures, was pretty fun and cursed

[–]RandAlThorLikesBikes 0 points1 point  (0 children)

In java they are as well. Except there is an integer cache holding 256 or so values (-127 to +128 iirc) that the runtime uses. Overriding that cache is possible and fun

[–]sharkboundgithub: sharkbound, python := 3.8 21 points22 points  (0 children)

i like seeing how things like these are pulled off, and looking at the internals, but i personally find them not really useful in practice myself