On 2021-08-25 00:48, Guido van Rossum wrote:
Hi Tim,
I'm sorry if this has been brought up before, but *aside from PEP 8* is
there anything wrong with using "if len(a)" for nonempty, or "if not
len(a)" for empty?
What is the cost of 'len'? If it's always O(1), then it's not a problem,
but if it's not O(1) (counting the items in a tree, for example) and
you're not interested in how many items there are but only whether
there's at least one, then...
It would seem to work for numpy and pandas arrays, and it works for
builtin sequences. Also, it has the advantage of being 100% backwards
compatible. :-)
Surely conforming to PEP 8 shouldn't need an addition to the language or
stdlib? Or does it not work?
On Tue, Aug 24, 2021 at 3:42 PM Tim Hoffmann via Python-ideas
<python-ideas@python.org <mailto:python-ideas@python.org>> wrote:
Ethan Furman wrote:
> 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.
Sorry, I think you got me wrong: The meaning and thus implementation
depends on the type (e.g. each container defines its own __len__()).
However the syntax for querying length that is still uniformly
`len(container)`. Likewise I'd like to have a uniform syntax for
emptiness, and not different syntax for different types (`if not
container` / `if len(array) == 0` / `if dataframe.empty`).
_______________________________________________
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/3GE7A7WQD6327IPE74IQQX5HHL4UWNOA/
Code of Conduct: http://python.org/psf/codeofconduct/