On Fri, Mar 2, 2018 at 12:20 PM, Chris Angelico <ros...@gmail.com> wrote:

> How often do you have a loop like this where you actually want to
> capture the exact condition? I can think of two: regular expressions
> (match object or None), and socket read (returns empty string on EOF).
> This simplified form is ONLY of value in that sort of situation; as
> soon as you want to add a condition around it, this stops working (you
> can't say "while do_something() is not _sentinel as x:" because all
> you'll get is True). And if you are looking for one specific return
> value as your termination signal, you can write "for x in
> iter(do_something, None):".

​For me, it's all the time.  Our geometry modeling database is
hierarchical, so you see things like this all over kernel, often with a lot
more code than just that one line calculating the cumulative scale factor:

>>> scale = self.scale
>>> parent = self.parent
>>> while parent:
>>>     scale​ *= parent.scale
>>>     parent = parent.parent  # The DRY part that I don't like...

which would turn into

>>> scale = self.scale
>>> parent = self
>>> while parent.parent as parent:
>>>     scale​ *= parent.scale
Python-ideas mailing list
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to