[Python-Dev] the not operator (and the not special method) (original) (raw)

Oren Tirosh oren-py-d@hishome.net
Fri, 4 Oct 2002 06:43:54 -0400


On Thu, Oct 03, 2002 at 07:22:15PM -0400, Aahz wrote:

On Thu, Oct 03, 2002, Brian Quinlan wrote: > > > shows that python doesn't call the not special method > > in a 'not' operator statement. > > Python calls the special nonzero method so check the truth value of > an object.

Mark McEahern provided the full answer: Python uses len, too.

This reminds me of an asymmetry between the bool type and other built-in types: the lack of a bool method.

Perhaps a bool method should be added to objects, just like int, str, long, float, complex and unicode.

If bool is not defined bool() would fall back to using nonzero and len, just like str() falls back to using repr when str is not defined.

Would this be done in CPython with a new tp_bool slot or special-cased like unicode and complex? Binary compatibility aside, I think it should be a slot. Boolean evaluation is a pretty basic operation.

Oren