"0 in [True,False]" returns True

Duncan Booth duncan.booth at invalid.invalid
Tue Dec 13 11:28:34 CET 2005


 wrote:

>> if the purpose of the return value is to indicate a Boolean rather than
>> an arbitrary integer.
>>
> True, but if that is the only reason, Two built-in value of
> True/False(0/1) serves the need which is what is now(well sort of). Why
> have seperate types and distinguish them ?
> 
>>>>True == 1
> True
>>>>True is 1
> False
> 

Within Python that would probably be sufficient, but some external 
libraries e.g. COM or XMLRPC make a distinction between integers and 
booleans, so it makes it more convenient if there is a defined way to 
distinguish between calling one overloaded method which takes an integer or 
another of the same name which take a boolean.

Before Python had a separate boolean type there was an implementation 
detail which mean that it was possible to distinguish the constants 0 and 1 
which were generated by a comparison from other constant 0 and 1 values. 
The python COM libraries used this 'feature'.

XMLRPC has its own Boolean class which is used in Python versions where 
boolean is not a builtin.

Another reason to have a boolean type is of course to provide a cast:

   def isNameSet(self):
      return boolean(self.name)

instead of:

   def isNameSet(self):
       if self.name:
          return True
       else:
          return False



More information about the Python-list mailing list