Created
November 22, 2016 14:27
-
-
Save chendi0x7C00/9c3abafedd2885504c1cc26663d62934 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # 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