Comparisons and sorting of a numeric class....

Ian Kelly ian.g.kelly at gmail.com
Tue Jan 13 01:22:50 EST 2015


On Mon, Jan 12, 2015 at 9:16 PM, Andrew Robinson
<andrew3 at r3dsolutions.com> wrote:
> Instead of pretending what if -- let's actually REPLACE python's built in
> bool class with an emulation that ALLOWS subclassing and THEN let's TEST my
> hypothesis that the assert statement you gave me can't tell the difference
> between bools any anthing else by it's actions...  ( ooooh ... another
> back-door that Guido forgot about... or perhaps purposely left open...)
>
> class bool( int ):
>     def __new__(self,data): return super( bool, self).__new__(self,
> [0,1][data!=0] )
>     def __repr__(self): return [ 'True', 'False' ][self>0]
>
> __builtins__.bool=bool
> __builtins__.True=bool( 1==1 )
> __builtins__.False=bool( 1==0 )

Your posts are so wordy that I'm not even going to try to respond to
all of them, but this is an interesting point that bears explanation.
The reason you can reassign True and False in Python 2.x is for
backward compatibility with scripts that predated the built-in bool
type and would do things like:

True = 1
False = 0

However, this longstanding wart was fixed in Python 3 by making True
and False keywords:

>>> True = 42
  File "<stdin>", line 1
SyntaxError: can't assign to keyword
>>> __builtins__.True = 42
  File "<stdin>", line 1
    __builtins__.True = 42
                    ^
SyntaxError: invalid syntax



More information about the Python-list mailing list