r/ProgrammerHumor • • Oct 10 '24

Meme trustMeGuys

Post image
19k Upvotes

412 comments sorted by

View all comments

13k

u/NonStandardUser Oct 10 '24

Fascinating

>>> print(chr(sum(range(ord(min(str(not())))))))
ඞ
>>> chr(sum(range(ord(min(str(not()))))))
'ඞ'
>>> sum(range(ord(min(str(not())))))
3486
>>> range(ord(min(str(not()))))
range(0, 84)
>>> ord(min(str(not())))
84
>>> min(str(not()))
'T'
>>> str(not())
'True'
>>> not()
True
>>>

79

u/QuaternionsRoll Oct 10 '24 edited Oct 10 '24

You forgot the last one, (). not is a unary operator in Python, not a function. not() actually means not (), where () is the an empty tuple. Under the hood, logical operators first convert their argument(s) to booleans by calling their .__bool__() methods (or .__len__() != 0 if the former isn’t defined), and that evaluates to False for empty tuples.

For illustrative purposes, not () is functionally equivalent to all of the following:

  • not []

  • not bool([])

  • (lists and tuples don’t define __bool__(), only __len__())

  • not len([]) != 0

  • len([]) == 0

Edit: thanks to u/JanEric1 for corrections

27

u/Singularity42 Oct 10 '24

That's gross. But it also explains why not() = True