class Boolean (was true = 1)

Cromwell, Jeremy jcromwell at ciena.com
Sat Jan 12 13:47:40 EST 2002


I'm sure I'm not the first to implement this, but...

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)

PythonWin 2.1 (#15, Apr 19 2001, 10:28:27) [MSC 32 bit (Intel)] on win32.
Portions Copyright 1994-2001 Mark Hammond (MarkH at ActiveState.com) - see
'Help/About PythonWin' for further copyright information.

>>> true
Boolean(1)
>>> false
Boolean(0)
>>> print true, false
True False
>>> true and 3
3
>>> true or 3
Boolean(1)
>>> false and 3
Boolean(0)
>>> false or 3
3
>>> true == 3
1
>>> 3 == true
1
>>> false == 3
0
>>> 3 == false
0
>>> false == 0
1
>>> false == None
1
>>> false == []
1
>>> false == (0,)
0
>>> true == [0]
1
>>> class something: pass
... 
>>> s = something()
>>> true == s
1


Only after I ran these examples did I realize that they should be True and
False (like None), even though I had it right in __repr__

All-this-one-handed-while-holding-an-infant-ly yrs,
> **********************************************************
> Jeremy Cromwell
> CIENA Core Switching Division
> jcromwell at ciena.com
> 
PS Fred says cgbtggggggggg5fvgfxf5t56gbvt6k7dsu uj78jm ok



-----Original Message-----
From: Mark McEahern [mailto:marklists at mceahern.com]
Sent: Saturday, January 12, 2002 8:51 AM
To: python-list at python.org
Subject: RE: true = 1


Joshua Muskovitz wrote:
> My guess would be because many things can be true besides the numeral 1.
>
> For example,
> >>> true = 1
> >>> if ["bar"]: print "foo"
>
> foo
> >>> if ["bar"] == true: print "foo"
>
> >>>

In other words, my proposal does not imply using the second method of
evaluating true nor does it preclude it.

// mark


-- 
http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list