[Python-Dev] PEP 572 and assert

Victor Stinner vstinner at redhat.com
Tue Jul 17 07:21:17 EDT 2018


2018-07-17 10:50 GMT+02:00 Serhiy Storchaka <storchaka at gmail.com>:
>     assert len(subdirs := list(path.iterdir())) == 0, subdirs
>
> Does PEP 572 encourages writing such code, discourages this, or completely
> forbids?

If I understood correctly Guido, Python the language must not prevent
developers to experiment various usage of assignement expressions. The
f-string has a similar design: anything even
f'{__import__("os").system("rm -rf /")}' is allowed.

It's more the role of linters like flake8 to warn against "bad practices".

I don't think that PEPs like f-string and assignment expressions must
include *coding styles*.

Sure, f-string and assignment expressions allow to write surprising
and bad code. But you don't them them to write crappy code :-)

I was strongly against f-string at the beginning because I immediately
predicted that people will start to call functions (with side effects)
in f-string... but I didn't see that happen. In fact, developers are
reasonable and use f-string when it's appropriate and safe.

Victor


More information about the Python-Dev mailing list