Last active
November 10, 2025 03:37
-
-
Save oshinko/1984b25868e5428e3e707ac21466db32 to your computer and use it in GitHub Desktop.
Convert to Boolean
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def boolify(value: object) -> bool: | |
| """ | |
| Convert to bool. | |
| >>> falsy = None, False, 0, b'', '', '0', 'false', 'off' | |
| >>> truthy = True, 1, b'\\0', '1', 'true', 'on', 'A' | |
| >>> any(boolify(x) for x in falsy) | |
| False | |
| >>> all(boolify(x) for x in truthy) | |
| True | |
| """ | |
| if isinstance(value, str) and value.lower() in set(('0', 'false', 'off')): | |
| return False | |
| return bool(value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment