seeking deeper (language theory) reason behind Python design choice

Marko Rauhamaa marko at pacujo.net
Thu May 10 20:29:57 EDT 2018


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.

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.


Marko



More information about the Python-list mailing list