Skip to content

Instantly share code, notes, and snippets.

@pmbrull
Created April 1, 2022 15:56
Show Gist options
  • Save pmbrull/3f144b4fdc39f006b5f46f69a3647e1a to your computer and use it in GitHub Desktop.
Save pmbrull/3f144b4fdc39f006b5f46f69a3647e1a to your computer and use it in GitHub Desktop.
Python truth 2
"""
help(int)
[...]
| __bool__(self, /)
| self != 0
[...]
help(str)
[...]
| __len__(self, /)
| Return len(self).
[...]
"""
# Let's apply the __bool__ logic for `int`
bool(10) # True
bool(0) # False
# Now, check the __len__ and boolean cast for `str`
len("hello") # 5
bool("hello") # True
len("") # 0
bool("") # False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment