Re: [Python-Dev] PEP 572 and assert

2018-07-17 Thread Steven D'Aprano
On Wed, Jul 18, 2018 at 03:41:08AM +1000, Steven D'Aprano wrote: > Besides, there is a legitimate use for assignment expressions in > assertions: > > assert (spam := something) > 2, 'not enough spam' > assert sequence[foo] == 999, 'sequence item isn't 999' Ah, obviously the index in th

Re: [Python-Dev] PEP 572 and assert

2018-07-17 Thread Barry Warsaw
On Jul 17, 2018, at 12:18, MRAB wrote: > Why use len(...) == 0 instead of not(...)? > >assert not(subdirs := list(path.iterdir())), subdirs Off-topic, but it’s a style thing. Some style guides (and my personal preference) require to be more explicit when checking for empty sequences. I’

Re: [Python-Dev] PEP 572 and assert

2018-07-17 Thread MRAB
On 2018-07-17 19:34, Tim Peters wrote: [Barry Warsaw] Thanks!  I thought it was cute.  It was just something that occurred to me as I was reviewing some existing code.  The intent wasn’t to use `subdirs` outside of the assert statement, but I’m warm to it because it means I don’

Re: [Python-Dev] PEP 572 and assert

2018-07-17 Thread Barry Warsaw
On Jul 17, 2018, at 11:34, Tim Peters wrote: > Assuming the result of list(path.iterdir()) can change over time (seems very > likely), > >assert len(list(path.iterdir())) == 0, list(path.iterdir()) > > _could_ end up both triggering and displaying an empty list in the exception > detail.

Re: [Python-Dev] PEP 572 and assert

2018-07-17 Thread Tim Peters
[Barry Warsaw] > Thanks! I thought it was cute. It was just something that occurred to me > as I was reviewing some existing code. The intent wasn’t to use `subdirs` > outside of the assert statement, but I’m warm to it because it means I > don’t have to do wasted work outside of the assert sta

Re: [Python-Dev] PEP 572 and assert

2018-07-17 Thread Barry Warsaw
On Jul 17, 2018, at 07:59, Guido van Rossum wrote: > > Personally I like Barry's example just fine -- assuming `subdirs` is not used > later, this feels like a good use case. Thanks! I thought it was cute. It was just something that occurred to me as I was reviewing some existing code. The

Re: [Python-Dev] PEP 572 and assert

2018-07-17 Thread Steven D'Aprano
On Tue, Jul 17, 2018 at 07:24:12PM +0300, Serhiy Storchaka wrote: > 17.07.18 18:48, Guido van Rossum пише: > >On Tue, Jul 17, 2018 at 8:28 AM, Serhiy Storchaka >> wrote: > >Should not the assert statement introduce an implicit lexical scope > >for preventing lea

Re: [Python-Dev] PEP 572 and assert

2018-07-17 Thread Guido van Rossum
On Tue, Jul 17, 2018 at 9:24 AM, Serhiy Storchaka wrote: > 17.07.18 18:48, Guido van Rossum пише: > >> On Tue, Jul 17, 2018 at 8:28 AM, Serhiy Storchaka > > wrote: >> Should not the assert statement introduce an implicit lexical scope >> for preventing leaking

Re: [Python-Dev] PEP 572 and assert

2018-07-17 Thread Chris Angelico
On Wed, Jul 18, 2018 at 2:24 AM, Serhiy Storchaka wrote: > 17.07.18 18:48, Guido van Rossum пише: >> >> On Tue, Jul 17, 2018 at 8:28 AM, Serhiy Storchaka > > wrote: >> Should not the assert statement introduce an implicit lexical scope >> for preventing leaking

Re: [Python-Dev] PEP 572 and assert

2018-07-17 Thread Serhiy Storchaka
17.07.18 19:25, Tim Peters пише: It is a win (to my eyes) if the code it replaced was     if __debug__:         subdirs = list(path.iterdir())         assert len(subdirs) == 0, subdirs in which case the semantics are the same in either spelling, with or without -O, but the spelling at the t

Re: [Python-Dev] PEP 572 and assert

2018-07-17 Thread Tim Peters
[Serhiy Storchaka] > 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).

Re: [Python-Dev] PEP 572 and assert

2018-07-17 Thread Serhiy Storchaka
17.07.18 18:48, Guido van Rossum пише: On Tue, Jul 17, 2018 at 8:28 AM, Serhiy Storchaka > wrote: Should not the assert statement introduce an implicit lexical scope for preventing leaking variables? I don't see why. As Chris said, side effects in asserts are

Re: [Python-Dev] PEP 572 and assert

2018-07-17 Thread Chris Barker via Python-Dev
> > I don't see why. As Chris said, side effects in asserts are nothing new > and > Indeed -- this new feature makes it easier to affect the local scope in all sorts of new places. It was decided that the additional complexity is worth it to make the language more expressive, and it was also decid

Re: [Python-Dev] PEP 572 and assert

2018-07-17 Thread Guido van Rossum
On Tue, Jul 17, 2018 at 8:28 AM, Serhiy Storchaka wrote: > 17.07.18 17:59, Guido van Rossum пише: > >> 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. >> > >

Re: [Python-Dev] PEP 572 and assert

2018-07-17 Thread Serhiy Storchaka
17.07.18 17:59, Guido van Rossum пише: 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. Shouldn't this problem be solved in the same way as for comprehensions? Shou

Re: [Python-Dev] PEP 572 and assert

2018-07-17 Thread Guido van Rossum
On Tue, Jul 17, 2018 at 1:50 AM, Serhiy Storchaka 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 depe

Re: [Python-Dev] PEP 572 and assert

2018-07-17 Thread Victor Stinner
2018-07-17 10:50 GMT+02:00 Serhiy Storchaka : > 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 var

Re: [Python-Dev] PEP 572 and assert

2018-07-17 Thread Antoine Pitrou
On Tue, 17 Jul 2018 18:56:36 +1000 Chris Angelico wrote: > On Tue, Jul 17, 2018 at 6:50 PM, Serhiy Storchaka 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 v

Re: [Python-Dev] PEP 572 and assert

2018-07-17 Thread Chris Angelico
On Tue, Jul 17, 2018 at 6:50 PM, Serhiy Storchaka 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 depen

[Python-Dev] PEP 572 and assert

2018-07-17 Thread Serhiy Storchaka
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 ou