[Python-3000] __nonzero__ vs. __bool__

Delaney, Timothy (Tim) tdelaney at avaya.com
Wed Nov 22 03:05:26 CET 2006


Terry Reedy wrote:

> 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?

Doesn't len() have a requirement that __len__ return an integer? If so,
perhaps it would be better if this were:

  def bool(obj):
      try:
          return obj.__bool__()
      except AttributeError:
          return len(obj) > 0

If I'm wrong (don't have the source available right now) then perhaps
len() *should* have this requirement.

Tim Delaney


More information about the Python-3000 mailing list