Skip to content

Instantly share code, notes, and snippets.

@chendi0x7C00
Created November 22, 2016 14:27
Show Gist options
  • Save chendi0x7C00/9c3abafedd2885504c1cc26663d62934 to your computer and use it in GitHub Desktop.
Save chendi0x7C00/9c3abafedd2885504c1cc26663d62934 to your computer and use it in GitHub Desktop.
# coding=UTF-8
class User(object):
def __init__(self, id):
self.id = id
def __nonzero__(self):
return self.id not in (None, 0)
def __len__(self):
# meaning less for User class
pass
def main():
u1 = User(1)
print(bool(u1)) # first check __nonzero__, then __len__ method
u2 = User(0)
print(bool(u2))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment