Skip to content

Instantly share code, notes, and snippets.

@DeaconDesperado
Last active December 23, 2015 18:29
Show Gist options
  • Save DeaconDesperado/6675588 to your computer and use it in GitHub Desktop.
Save DeaconDesperado/6675588 to your computer and use it in GitHub Desktop.
Python conditional truthiness is len by default
class Dummy(object):
def __init__(self,foo):
self.foo = foo
def __len__(self):
#This will make me always falsy, define and __nonzero__ method!
return 0
def __gt__(self,other):
return self.foo > other
def __lt__(self,other):
return self.foo < other
def __eq__(self,other):
return self.foo == other
d = Dummy(200)
if d:
print 'This wont ever print, dummy! Define a __nonzero__ method!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment