[Python-Dev] PEP 572 and assert

Guido van Rossum guido at python.org
Tue Jul 17 10:59:35 EDT 2018


On Tue, Jul 17, 2018 at 1:50 AM, Serhiy Storchaka <storchaka at gmail.com>
wrote:

> Recently Barry shown an example:
>
>     assert len(subdirs := list(path.iterdir())) == 0, subdirs
>
> It looks awful to me. It looks even worse than using asserts for
> validating the user input. The assert has a side effect, and it depends on
> the interpreter option (-O). Even if subdirs is not used outside of the
> assert *now*, it is easy to introduce an error later, and it is hard to
> notice it if tests are not ran with the -O option regularly.
>
> Does PEP 572 encourages writing such code, discourages this, or completely
> forbids?
>

The PEP has no specific opinion except it is not forbidden.

Personally I like Barry's example just fine -- assuming `subdirs` is not
used later, this feels like a good use case.

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20180717/f77688c2/attachment.html>


More information about the Python-Dev mailing list