homepage

This issue tracker has been migrated to GitHub , and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: collections.abc.Iterable don't implement __bool__
Type: enhancement Stage: resolved
Components: Library (Lib) Versions:
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: r.david.murray, yoch.melka
Priority: normal Keywords:

Created on 2015年10月29日 19:38 by yoch.melka, last changed 2022年04月11日 14:58 by admin. This issue is now closed.

Messages (2)
msg253690 - (view) Author: yoch (yoch.melka) Date: 2015年10月29日 19:38
collections.abc.Iterable don't implement the __bool__ method. This may seriously degrade performance in case __len__ is not efficient.
I suggest to implement it as :
class Iterable:
 ...
 def __bool__(self):
 try:
 next(iter(self))
 return True
 except StopIteration:
 return False
msg253691 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015年10月29日 19:51
This is as designed. The only method needed for something to qualify as an Iterable is __iter__, thus this is the only method defined on the ABC. If you want your Iterable to be usable in a boolean test, add the __bool__ method to your concrete class.
In fact, you will note that no ABCs define __bool__.
Likewise __len__ is not part of being an Iterator, and in fact in the general case Iterators do not have a len (a given Iterator might be infinite, or it might not be practical to calculate the len).
History
Date User Action Args
2022年04月11日 14:58:23adminsetgithub: 69699
2015年10月29日 19:51:00r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg253691

resolution: rejected
stage: resolved
2015年10月29日 19:38:24yoch.melkacreate

AltStyle によって変換されたページ (->オリジナル) /