PEP 285: Adding a bool type

Petr Prikryl Answer.via.news.please.prikryl at skil.nospam.cz
Thu Apr 11 04:21:27 EDT 2002


"Andreas Kostyrka" wrote...
>
> Alexis Layton <alexis.layton at post.harvard.edu> wrote:
>
> > As for int-compatibility, were we designing Python
> > from the ground up it might be the case that it
> > should be designed as a pure truth type. But we
> > are constrained by Pythons 1-2.2.x so True has to
> > be truly-comparable to 1.

This was also the problem in C++ where the
comparability of bool and int had to be solved
basically for the same reasons.  In C++, new
built-in bool type is strict and still it is
possible to mix it with int.

The mechanism for mixing it is called *implicit
conversion* that is done for the recognized
*contex* of the expression.  In other words, when
boolean value is found where integer is expected,
the implicit conversion of true to 1 is done
(false to zero).  When the opposite is required,
the implicit conversion from zero to false or !=0
to true is done.  When the conversion is
suspicious (i.e. when the compiler guessed that
the value is used in some very unexpected way --
say (35*false) -- then the warning may appear).

This way, it is possible to introduce strict bool
and not to break the old code.

> Nope. It must be truly-comparable to any non-zero
> object. As it happens this is what Python uses now
> as definition of Truth :) False OTOH must
> truly-comparable to ["",u"",None,{},[]] (and
> whatever I've just forgot).
>
> I personally have often written code that uses one
> variable to double as "boolean indicator" and
> "value storage":
>
> founditem=None
> while not founditem:
> ....
> if ....:
> founditem="VALUE"
>
> print "Found item with value:",founditem

This code would be preserved if Python itroduced
the strict bool together with implicit conversion
of int to bool and bool to int.

See details at my original "True/False story...
(PEP 285)".  The HTML version of the original is
temporatily available at
http://www.skil.cz/test/TrueFalseStory.html

Petr
--
Petr Prikryl (prikrylp at skil dot cz)





More information about the Python-list mailing list