flâneur — a map of the web's best reading

pablogsal/python-horror-show: Strange and odd python snippets explained

github.com · 1,428 words · saved by 1 readers

Here you will find a collection of strange and odd python snippets showing apparent odd behavior. The purpose of these scripts is to mess with your head but some people have reported strange new Python knowledge as a secondary effect. From "Integer Objects": 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. :-) We can check this thing using the id operator: The brief explanation is that as in Python everything is an object, each time you use a number (integer, float...) it must be created so this could be very inefficient. So what Python does is pre-allocate integers from -5 to 256 because these are often used. The last trick (a = 300; b = 300) is interpreter-dependent, but in the basic Python interpreter (among others) as the two

Explore this link on the map →

saved by

related reading