How to make a custom type "true" or not, in Python 1.5.2

Warren Postma embed at geocities.com
Mon Feb 19 16:19:37 EST 2001


I am writing a little event object, here is the PyTypeObject declaration:

static PyTypeObject win32eventtype = {
 PyObject_HEAD_INIT(&PyType_Type)
 0,   /*ob_size*/
 "win32event",   /*tp_name*/
 sizeof(win32eventobject), /*tp_basicsize*/
 0,   /*tp_itemsize*/
 /* methods */
 (destructor)win32event_dealloc, /*tp_dealloc*/
 0,   /*tp_print*/
 (getattrfunc)win32event_getattr, /*tp_getattr*/
 (setattrfunc)win32event_setattr, /*tp_setattr*/
 0,   /*tp_compare*/
 (reprfunc)win32event_repr,   /*tp_repr*/
 0,   /*tp_as_number*/
 0,   /*tp_as_sequence*/
 0,   /*tp_as_mapping*/
 0,   /*tp_hash*/
};

Now, how do I make it true or false, it appears that all C Type object
instances are True, because They Are A Thing, not Nothing, but what if you
want to make something that can be a Logically False Entity?  Do I have to
implement the sequence protocol, and implement len(x)?  It doesn't seem that
it calls my getattr function to check anything. It's just plain true.

This is using Python 1.5.2, on Win32.

Warren Postma






More information about the Python-list mailing list