[Python-3000] __nonzero__ vs. __bool__

Guido van Rossum guido at python.org
Wed Nov 22 21:16:49 CET 2006


Once all who care agree on the patch, feel free to check it in without
my review.

On 11/22/06, Walter Dörwald <walter at livinglogic.de> wrote:
> Jack Diederich wrote:
>
> > On Wed, Nov 22, 2006 at 09:36:09AM +0100, Georg Brandl wrote:
> >> Terry Reedy schrieb:
> >>> "Brett Cannon" <brett at python.org> wrote in message
> >>> news:bbaeab100611211232r53de3c33p915bee7b49dbdf9a at mail.gmail.com...
> >>>
> >>> Why can't the fallback usage just pass the return value from __len__ to
> >>> bool() (forget the C function name) and return that result?  It's just like
> >>> doing::
> >>>
> >>>   def bool(obj):
> >>>       try:
> >>>           return obj.__bool__()
> >>>       except AttributeError:
> >>>           return bool(len(obj))
> >>> ------------
> >>>
> >>> If an object without __bool__ returned itself as its length, this would be
> >>> an infinite loop, at least in this Python version.  Do we worry about
> >>> something so crazy?
> >> The length would have to be an integer, and this would have to be checked.
> >>
> >
> > It looks like the regular checks are happening on __len__ methods
> > anyway so the explicit int check in slot_nb_bool is redundant.
> > This is the first time I've looked at the new slots in py3k so
> > feel free to correct. (using bool4.patch)
> >
> > sprat:~/src/py3k-rw> ./python
> > Python 3.0x (p3yk:52823M, Nov 22 2006, 11:57:34)
> > [GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2
> > Type "help", "copyright", "credits" or "license" for more information.
> >>>> class A(object):
> >   def __len__(self):
> >     return -1
> >
> > a = A()
> > print bool(a)
> >   ... ... ... >>> >>> Traceback (most recent call last):
> >   File "<stdin>", line 1, in <module>
> > ValueError: __len__() should return >= 0
>
> OK, I've updated the patch: bugs.python.org/1600346
>
> Servus,
>    Walter
> _______________________________________________
> Python-3000 mailing list
> Python-3000 at python.org
> http://mail.python.org/mailman/listinfo/python-3000
> Unsubscribe: http://mail.python.org/mailman/options/python-3000/guido%40python.org
>


-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-3000 mailing list