On Thu, May 10, 2018 at 7:10 PM, Chris Angelico <[email protected]> wrote:
> On Fri, May 11, 2018 at 10:29 AM, Marko Rauhamaa <[email protected]> wrote:
>> Chris Angelico <[email protected]>:
>>
>>> 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.
In what way does "while True" in the general case pretend to be an
infinite loop? The break / return is right there for anyone to see.
Would you also contend that generator functions are wrong because they
pretend to be normal functions?
def totally_not_a_generator(n):
while True:
if n % 2 == 0:
n //= 2
else:
n = n * 3 + 1
stealthily = n
yield stealthily
if n == 1:
return n
py> print(totally_not_a_generator(42)) # Lies!
<generator object totally_not_a_generator at 0x77fe4d78c0f8>
--
https://mail.python.org/mailman/listinfo/python-list