On 24Aug2021 06:55, tim.hoffmann@mailbox.org
Ethan Furman wrote:
"has element-wise operations" protocol or an is_empty protocol. I consider emptiness-check a basic concept that should be consistent and easy to use across containers.
Python has an emptiness-check and numpy chose to repurpose it -- that is not Python's problem nor a shortcoming in Python.
Python does not have an emptiness-check. Empty containers map to False and we suggest in PEP8 to use the False-check as a stand in for the emptiness-check. But logically falsiness and emptiness are two separate things. Numpy (IMHO with good justification) repurposed the False-check, but that left them without a standard emptiness check.
In my mind PEP8 says that the emptiness check should be expressed as a False-check for Pythonic containers. That is a good thing to me. It does not mean we've no notion of emptiness, it says that if you've got something which can be empty it should be possible to check that by doing a False-check. And _that_ implies that for containers, the False-check _is_ emptiness. Defined, to me. Numpy has made a different decision, probably because a heap of operators on their arrays map the operator onto the array elements. To them, that is useful enough to lose some Python-idiom coherence for the wider gain _in that special domain_. If we're chasing rough edges, consider queue.Queue:
from queue import Queue Q = Queue() Q.empty() True if Q: print("Q is true") ... Q is true
I would often like to treat Queues as a container of queued items,
basicly because I'd like to be able to probe for the presence of queued
items via the emptiness idiom. But I can't. It does has a .empty()
method.
I don't even know what my point is here :-(
But I am definitely -1 on weaking the bool(container) idiom as a test
for empty/nonempty, and also for asking every container implementation
on the planet to grow a new is_empty() method.
Cheers,
Cameron Simpson