Skip to content

Instantly share code, notes, and snippets.

@mrdrozdov
Created July 29, 2015 16:45
Show Gist options
  • Save mrdrozdov/31c4cae19c4c5e8d517d to your computer and use it in GitHub Desktop.
Save mrdrozdov/31c4cae19c4c5e8d517d to your computer and use it in GitHub Desktop.
truthiness in python
class WithTruthiness(object):
"""docstring for WithoutBool"""
def __init__(self, arg):
super(WithTruthiness, self).__init__()
self.arg = arg
def __nonzero__(self):
if self.arg:
return True
else:
return False
def __bool__(self):
return self.__nonzero__()
class WithoutTruthiness(object):
"""docstring for WithoutTruthiness"""
def __init__(self, arg):
super(WithoutTruthiness, self).__init__()
self.arg = arg
if __name__ == '__main__':
hasNotTruthinessTrue = WithoutTruthiness(True)
hasNotTruthinessFalse = WithoutTruthiness(False)
hasTruthinessTrue = WithTruthiness(True)
hasTruthinessFalse = WithTruthiness(False)
if hasNotTruthinessTrue:
print "hasNotTruthinessTrue is True"
if hasTruthinessTrue:
print "hasTruthinessTrue is True"
if hasNotTruthinessFalse:
print "hasNotTruthinessFalse is True"
if hasTruthinessFalse:
print "hasTruthinessFalse is True"
"""
hasNotTruthinessTrue is True
hasTruthinessTrue is True
hasNotTruthinessFalse is True
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment