Created
July 29, 2015 16:45
-
-
Save mrdrozdov/31c4cae19c4c5e8d517d to your computer and use it in GitHub Desktop.
truthiness in python
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 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