On 8/24/21 3:03 PM, Tim Hoffmann via Python-ideas wrote:
> **How do you check if a container is empty?**
>
> IMHO the answer should not depend on the container.
I think this is the fly in the ointment -- just about everything, from len() to bool(), to add, to iter() /all/ depend
on the container -- even equality depends on the container. `and`, `or`, and `not` partially depend on the container
(via bool()). Only `is` is truly independent.
> Not a solution:
> 0) The current `if not seq` syntax. "check Falsiness instead of emptiness" is
a
> simplification, which is not always possible.
>
> Possible solutions:
>
> 1) Always use `if len(seq) == 0`. I think, this would works. But would we
want to
> write that in PEP-8 instead of `if not seq`? To me, this feels a bit too
low level.
>
> 2) A protocol would formalize that concept by building respective syntax into
the
> language. But I concede that it may be overkill.
>
> 3) The simple solution would be to add `is_empty()` methods to all stdlib
containers
> and encourage third party libs to adopt that convention as well. That
would give a
> uniform syntax by convention.
>
> Reflecting the discussion in this thread, I now favor variant 3).
And since (3) is a method on the container, it absolutely "depends on the
container".
--
~Ethan~
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/O2QZMKYC6FLKADYKS4UPCINPKJ65DCZV/
Code of Conduct: http://python.org/psf/codeofconduct/