Skip to content

Instantly share code, notes, and snippets.

@MSeifert04
Last active March 12, 2016 03:58
Show Gist options
  • Save MSeifert04/19eb22199a8f035e5272 to your computer and use it in GitHub Desktop.
Save MSeifert04/19eb22199a8f035e5272 to your computer and use it in GitHub Desktop.
bool evaluation
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