<div dir="ltr"><div class="gmail_quote"><div dir="ltr">[Serhiy Storchaka]</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Recently Barry shown an example:<br>
<br>
assert len(subdirs := list(path.iterdir())) == 0, subdirs<br>
<br>
It looks awful to me. It looks even worse than using asserts for <br>
validating the user input. The assert has a side effect, and it depends <br>
on the interpreter option (-O). Even if subdirs is not used outside of <br>
the assert *now*, it is easy to introduce an error later, and it is hard <br>
to notice it if tests are not ran with the -O option regularly.<br></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> </blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Does PEP 572 encourages writing such code, discourages this, or <br>
completely forbids?<br></blockquote><div><br>The body of the PEP specifies semantics. My Appendix A gives some _opinions_ about "good" and "bad" uses, which boil down to "if it's not obviously at least a little win, don't use it".<br><br>I can't really guess whether the above is an obvious win or not without context. It is a win (to my eyes) if the code it replaced was<br><br> if __debug__:</div><div> subdirs = list(path.iterdir())</div><div> assert len(subdirs) == 0, subdirs<br><br>in which case the semantics are the same in either spelling, with or without -O, but the spelling at the top is less annoying ;-)<br><br></div></div></div>