seeking deeper (language theory) reason behind Python design choice

Chris Angelico rosuav at gmail.com
Thu May 10 21:10:12 EDT 2018


On Fri, May 11, 2018 at 10:29 AM, Marko Rauhamaa <marko at pacujo.net> wrote:
> Chris Angelico <rosuav at gmail.com>:
>
>> But for the loop itself, you absolutely CAN write this more logically.
>> I'll take your second version as a template:
>>
>>     def split_cmd(self, cmd):
>>         args = []
>>         while (match := self.TERM_PTN.match(cmd)) is not None:
>>             args.append(match.group('term'))
>>             if not match.group('sep'):
>>                 verb = args.pop(0).upper()
>>                 return verb, args
>>             cmd = cmd[match.end(0):]
>>         return None, None
>>
>> And, if this is actually a regex, "is not None" is unnecessary:
>>
>> while match := self.TERM_PTN.match(cmd):
>>
>> Now do you understand what I mean about putting the condition into the
>> loop header?
>
> Thanks, but no thanks. The "while True" idiom beats that one hands down.

Because you're used to it? Or because it's somehow more logical to
pretend that this is an infinite loop? Explain in more detail.

> As for the "is not None" test, I generally want to make it explicit
> because
>
>  1. that's what I mean and
>
>  2. there's a chance in some context of confusing None with other falsey
>     values.
>

With the standard library re module, there is no such chance. So it's
pointlessly explicit about something that won't ever happen.

ChrisA



More information about the Python-list mailing list