Last active
March 12, 2016 03:58
-
-
Save MSeifert04/19eb22199a8f035e5272 to your computer and use it in GitHub Desktop.
bool evaluation
This file contains 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
class Test(object): | |
def __init__(self, var): | |
self.var = var | |
def __bool__(self): | |
print 'called it.' | |
return bool(self.var) | |
a = Test(10) | |
if a: # This will call a.__bool__ | |
pass | |
# called it. | |
if not a: | |
pass | |
# called it. | |
if a is None: # is only ever True if before "a = None". But beware "if not None == True" | |
pass | |
# Nothing happens |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment