ACCEPTED: PEP 285

Ralf Juengling juenglin at informatik.uni-freiburg.de
Thu Apr 4 06:35:46 EST 2002


"Terry Reedy" <tejarex at yahoo.com> writes:

> > Clarification
> >
> >     This PEP does *not* change the fact that almost all object types
> >     can be used as truth values.  For example, when used in an if
> >     statement, an empty list is false and a non-empty one is true;
> >     this does not change and there is no plan to ever change this.
> 
> almost? all?  I am under the impression that 'if x:' is universally
> valid, and consider that one of the good features of Python's prime
> dichotomy system.

Hi,

this might have been brought up already (I didn't follow the whole
discussion): Currently, when an object x is tested as in 
'if x:' or 'if not x:'

the interpretation of x as a boolean is done by PyObject_IsTrue()
at the very end. PyObject_IsTrue() itself tries to interpret the
given object as a number, then (if this fails) as a mapping, 
finally as a sequence. Here is where the something/nothing semantic
roots in.

But what about user-defined types? 

I think it should be specified by the type itself how to interpret
its instances in a boolean context. Thus, there should be a 
further slot 'tp_as_bool', a special method '__asbool__', respectively,
and this method should be invoked by PyObject_IsTrue(). If it were
not defined, an exception was raised.

Regards,
Ralf




More information about the Python-list mailing list