[Python-3000] __nonzero__ vs. __bool__
Georg Brandl
g.brandl at gmx.net
Wed Nov 22 09:36:09 CET 2006
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.
Georg
More information about the Python-3000
mailing list