pablogsal/python-horror-show: Strange and odd python snippets explained
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
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. Hidden memory things >>> a = 5 >>> b = 5 >>> a is b True >>> a = -4 >>> b = -4 >>> a is b True >>> a = 300 >>> b = 300 >>> a is b False >>> a = 300; b = 300 >>> a is b True 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…
saved by
related reading
- Python representation of negative integers - Stack Overflowstackoverflow.com
- 9. Classes — Python 3.14.6 documentationdocs.python.org
- cpython/Objects/abstract.c at 3.10 · python/cpython · GitHubgithub.com
- Python's Array: Working With Numeric Data Efficiently – Real Pythonrealpython.com
- styleguide | Style guides for Google-originated open-source projectsgoogle.github.io
- python's pre-declared constants are kinda weirdsebsite.pw
- Programming Thinkingyoutube.com
- class - Why do Python classes inherit object? - Stack Overflowstackoverflow.com
- python - Why is using 'eval' a bad practice? - Stack Overflowstackoverflow.com
- Using PyTorch + NumPy? You're making a mistake. · Tanel Pärnamaatanelp.github.io
- Python基本語法 - HackMDhackmd.io
- GitHub - srush/Tensor-Puzzles: Solve puzzles. Improve your pytorch.github.com