Python equivalent to a C trick

Peter Hansen peter at engcorp.com
Tue Aug 10 08:45:00 EDT 2004


Sam Holden wrote:

> But I don't know if int(False)==0 and int(True)==1 are actually
> guaranteed. And I wouldn't use it since it's ugly (IMHO).

 From the language ref at http://docs.python.org/ref/types.html#l2h-37

  Booleans
  These represent the truth values False and True. The two objects
  representing the values False and True are the only Boolean objects.
  The Boolean type is a subtype of plain integers, and Boolean values
  behave like the values 0 and 1, respectively, in almost all contexts,
  the exception being that when converted to a string, the strings
  "False" or "True" are returned, respectively.

So yes, it's fully guaranteed.



More information about the Python-list mailing list