class Boolean (was true = 1)

Brian Quinlan brian at sweetapp.com
Sat Jan 12 14:35:40 EST 2002


Jeremy Cromwell wrote:
> class Boolean:
>     def __init__(self, truth=1):
>         self.truth = not not truth
>     def __eq__(self, other):
>         return self.truth != (not other)
>     def __str__(self):
>         return ["False","True"][self.truth]
>     def __repr__(self):
>         return "Boolean(%d)" % self.truth
>     def __len__(self):
>         return self.truth
> 
> true = Boolean(1)
> false = Boolean(0)

You should probably change __len__ to __nonzero__. That way people won't
be fooled into thinking that your class is a container-like thing.

Cheers,
Brian





More information about the Python-list mailing list