I agree with Guido. The only problem here is third-party libraries that don't use bool() to indicate emptiness. If you need to support those, use len(). But this doesn't mean a change to the standard library, because those third-party libraries are, well, third-party. We don't need a more explicit way to specify emptiness. bool(seq) is fine.
On Wed, Aug 25, 2021, 8:05 AM Guido van Rossum <[email protected]> wrote: > My conclusion is that you should ignore PEP 8 for your use case and write > “if len(a) == 0”. > > On Wed, Aug 25, 2021 at 06:13 Tim Hoffmann via Python-ideas < > [email protected]> wrote: > >> Guido van Rossum wrote: >> > So then the next question is, what's the use case? What code are people >> > writing that may receive either a stdlib container or a numpy array, and >> > which needs to do something special if there are no elements? Maybe >> > computing the average? AFAICT Tim Hoffman (the OP) never said. >> >> There's two parts to the answer: >> >> 1) There functions e.g. in scipy and matplotlib that accept both numpy >> arrays and lists of flows. Speaking from matplotlib experience: While >> eventually we coerce that data to a uniform internal format, there are >> cases in which we need to keep the original data and only convert on a >> lower internal level. We often can return early in a function if there is >> no data, which is where the emptiness check comes in. We have to take extra >> care to not do the PEP-8 recommended emptiness check using `if not data`. >> >> 2) Even for cases that cannot have different types in the same code, it >> is unsatisfactory that I have to write `if not seq` but `if len(array) == >> 0` depending on the expected data. IMHO whatever the recommended syntax for >> emptiness checking is, it should be the same for lists and arrays and >> dataframes. >> _______________________________________________ >> 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/Q6KZEXFLJ6TEFSDQM3SXXIVGNFNURPYT/ >> Code of Conduct: http://python.org/psf/codeofconduct/ >> > -- > --Guido (mobile) > _______________________________________________ > 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/JVNSGTXFPCODPGEPX2N4SWB7LIMPGTVS/ > Code of Conduct: http://python.org/psf/codeofconduct/ >
_______________________________________________ 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/DT2LCRXU5PNEJTM53CMFLELVZW7DCCJJ/ Code of Conduct: http://python.org/psf/codeofconduct/
