[Python-Dev] For review: PEP 285: Adding a bool type (original) (raw)

M.-A. Lemburg mal@lemburg.com
Fri, 08 Mar 2002 17:31:28 +0100


Guido van Rossum wrote:

I'll let this short and sweet PEP speak for itself. http://python.sourceforge.net/peps/pep-0285.html PEP: 285 Title: Adding a bool type Version: Revision:1.1Revision: 1.1 Revision:1.1 Last-Modified: Date:2002/03/0815:38:37Date: 2002/03/08 15:38:37 Date:2002/03/0815:38:37 Author: guido@python.org (Guido van Rossum) Status: Draft Type: Standards Track Created: 8-Mar-2002 Python-Version: 2.3 Post-History: 8-Mar-2002 (python-dev) Abstract This PEP proposes the introduction of a new built-in type, bool, with two constants, False and True. The bool type would be a straightforward subtype (in C) of the int type, and the values False and True would behave like 0 and 1 in most respects (e.g. False==0 and True==1 would be true) except repr() and str(). All built-in operations that conceptually return a Boolean result will be changed to return False or True instead of 0 or 1; for example, comparisons and the "not" operator. Rationale Most languages eventually grow a Boolean type; even C99 has one. It's useful to be able to tell from a function result that the outcome has Boolean semantics.

+1.

Specification

The following Python code specifies most of the properties of the new type: class bool(int): def new(cls, val=0, create=0): if create: # This is nor part of the spec, # just a hack to bootstrap False and True return int.new(cls, not not val) elif val: return True else: return False def repr(self): if self: return "True" else: return "False" str_ = _repr

I don't like this: it will break too much code since there are already two singletons Py_True and Py_False available in Python and these return 1 / 0 resp.

def and(self, other): if isinstance(other, bool): return bool(int(self) & int(other)) else: return NotImplemented

rand_ = _and def or(self, other): if isinstance(other, bool): return bool(int(self) | int(other)) else: return NotImplemented ror_ = _or def xor(self, other): if isinstance(other, bool): return bool(int(self) ^ int(other)) else: return NotImplemented rxor_ = _xor False = bool(0, create=1) True = bool(1, create=1)

Please adjust Py_True and Py_False (at C level) to be identical to these constants.

Issues

Because the repr() or str() of a bool value is different from an int value, some code (e.g. doctest-based unit tests) may fail. How much of a backwards compatibility problem this will be, I don't know. If we find this is a real problem, we could add a command-line option to make False and True aliases for 0 and 1 and bool an alias for int.

Please, no !

Ideal would be to make bool a subtype of int and True and False two singletons created from 1 and 0 having the bool type: that's simple and backwards compatible.

One idea for a possible addition:

Add boolean support to the array module so that it becomes possible to create arrays of bits.

Copyright

This document has been placed in the public domain. Local Variables: mode: indented-text indent-tabs-mode: nil fill-column: 70 End: --Guido van Rossum (home page: http://www.python.org/~guido/)


Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev

-- Marc-Andre Lemburg CEO eGenix.com Software GmbH


Company & Consulting: http://www.egenix.com/ Python Software: http://www.egenix.com/files/python/