On Tue, 24 Aug 2021 at 23:06, Tim Hoffmann via Python-ideas
<python-ideas@python.org> wrote:
>
> I also have the feeling that this is going round in circles. So let me get 
> back to the core question:
>
> **How do you check if a container is empty?**
>
> IMHO the answer should not depend on the container. While emptiness may mean 
> different things for different types. The check syntax can and should still 
> be uniform.

I will note that if we take things to extremes, that constraint simply
cannot be adhered to - types can define bool, len, and any other
special method we care to invent, however they like. With that in
mind, I'd argue that if a collection defines bool and len in such a
way that "not bool(c)" or "len(c) == 0" doesn't mean "c is empty",
then it is a special case, and has deliberately chosen to be a special
case.

Yes, I know that makes numpy arrays and Pandas dataframes special
cases. As I said, they have deliberately chosen to not follow normal
conventions. Take it up with them if you care to. (IMO there's no
point - they have reasonable justifications for their choices, and
it's too late to change anyway).

Based on the "obvious" intent of the classes in collections.abc, I'd
say that if you test "len(c) == 0" then you can reasonably say that
you cover all collections. If you want to support the weird
multi-dimensional zero-sized numpy arrays that have len != 0, then
special case them. But frankly, I'd wait until a user comes up with a
reason why you need to support them, who can tell you what they expect
your code to *do* with them in the first place... Again, practical use
cases rather than abstract questions.

Paul
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/LSYLPDJ5VNPZR2W3T7PPP43CY7GZAXLL/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to