Curated list of all the hidden jokes, easter eggs, secrets and the lesser known things in Python
>>> class subclass_of_bool(bool):
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: type 'bool' is not an acceptable base type
>>> 0xfor~d
15
Now try tipping the 0xfedor-a 🎩
π = 3.1415
area = π * r ** 2
>>> p = 256
>>> q = 256
>>> p is q
True
>>> p = 257
>>> q = 257
>>> p is q
False
python -m "calendar" 2021 7
For options: python -m "calendar" --help
import this
import __hello__
from __future__ import braces
File "<stdin>", line 1
SyntaxError: not a chance
>>> hash(float('inf'))
314159
>>> hash(float('nan'))
0
import antigravity
>>> from antigravity import geohash
>>> print(geohash(37.421542, -122.085589, b'2005-05-26-10458.68'))
# 37.857713 -122.544543
>>> from __future__ import barry_as_FLUFL
>>> 1 != 3
File "<stdin>", line 1
1 != 3
^
SyntaxError: with Barry as BDFL, use '<>' instead of '!='
>>> 1 <> 3
True
>>> __peg_parser__
File "<stdin>", line 1
__peg_parser__
^
SyntaxError: You found it!
Note: This will work only in 3.9 and will be removed in 3.10 along with the LL(1) parser with the introduction of the new PEG parser.
>>> import types
>>> help(types.CodeType)
# 1 condition
['cond1 is false', 'cond1 is true'][cond1]
# 2 conditions
['both false', 'cond1 is true', 'cond2 is true', 'both true'][2*cond2 + cond1]
def loop(array):
if len(array) == 0:
return 0
else:
# do smth with array[0] for example print(array[0])
print(array[0])
return loop(array[1:])
loop([1,2,3,'a string'])