[Python-Dev] C API for the bool type?

Fred L. Drake, Jr. fdrake at acm.org
Thu Mar 24 20:00:19 CET 2005

On Monday 21 March 2005 04:54, Bo Thorsen wrote:
 > If this is not the correct place to post this problem, I apologize. In
 > that case, please be gentle and point me to a better mailing list.

You should be able to get answers to this kind of question on comp.lang.python 
(aka python-list at python.org).  But I'll give you this one for free.  :-)

 > I'm coding a text editor in Qt that uses Python for macros. The problem I
 > have is that want to use the bool type introduced in 2.3, but I can't see
 > how to do this. On http://docs.python.org/api/arg-parsing.html the format
 > units are described, but no bool is there.

There is no format character for boolean, and you don't actually need one.

Recall that in Python, all objects have some interpretation as a truth value.  
The easiest way to check this from C code is to pass the object to 
PyObject_IsTrue() or PyObject_Not() (depending on the sense of the test you 
want, and how you want your code to read).  The argument can be retrieved in 
your PyArg_ParseTuple*() call using the "O" format character.

 > If it doesn't exist, I guess I should use the int format unit and call the
 > http://docs.python.org/api/boolObjects.html functions to see if this is
 > actually a bool or not?

In most cases, you really don't care if the object is actually a bool.  The 
recipe above will also work in older versions of Python.  That lets you use 
bools for the reason they were really introduced: to enhance readability.


Fred L. Drake, Jr.  <fdrake at acm.org>

More information about the Python-Dev mailing list