All code and no narrative, this one. Here's the Python 3 equivalent of the Groovy code from the previous article (CFML (or probably LuceeLang) and what constitutes "The Truth").
print("none")
if None:
print("truthy")
else:
print("falsy")
print("=====================")
print("empty string")
if "":
print("truthy")
else:
print("falsy")
print("=====================")
print("non-empty string")
if "0":
print("truthy")
else:
print("falsy")
print("=====================")
print("zero")
if 0:
print("truthy")
else:
print("falsy")
print("=====================")
print("non-zero")
if -1:
print("truthy")
else:
print("falsy")
print("=====================")
print("empty list")
if []:
print("truthy")
else:
print("falsy")
print("=====================")
print("non-empty list")
if [None]:
print("truthy")
else:
print("falsy")
print("=====================")
print("empty dictionary")
if {}:
print("truthy")
else:
print("falsy")
print("=====================")
print("non-empty dictionary")
if {"key":None}:
print("truthy")
else:
print("falsy")
print("=====================")
class Test:
def __init__(self, value):
self.value = value
def __bool__(self):
return self.value == "truthy"
print("truthy object")
test = Test("truthy")
if test:
print("truthy")
else:
print("falsy")
print("=====================")
print("non-truthy object")
if Test("anything else"):
print("truthy")
else:
print("falsy")
print("=====================")
Output:
>python truthy.py
none
falsy
=====================
empty string
falsy
=====================
non-empty string
truthy
=====================
zero
falsy
=====================
non-zero
truthy
=====================
empty list
falsy
=====================
non-empty list
truthy
=====================
empty dictionary
falsy
=====================
non-empty dictionary
truthy
=====================
truthy object
truthy
=====================
non-truthy object
falsy
=====================
>
none
falsy
=====================
empty string
falsy
=====================
non-empty string
truthy
=====================
zero
falsy
=====================
non-zero
truthy
=====================
empty list
falsy
=====================
non-empty list
truthy
=====================
empty dictionary
falsy
=====================
non-empty dictionary
truthy
=====================
truthy object
truthy
=====================
non-truthy object
falsy
=====================
>
The most interesting thing in this is
__init__
and __bool__
? FFS. I've found a language even worse than PHP for its shit function names and underscore usage, it seems.More Guinness, pls...
--
Adam