[Python-ideas] PEP 572 version 2: Statement-Local Name Bindings

Eric Fahlgren ericfahlgren at gmail.com
Fri Mar 2 16:18:42 EST 2018


On Fri, Mar 2, 2018 at 12:20 PM, Chris Angelico <rosuav at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180302/8d6f18f3/attachment-0001.html>


More information about the Python-ideas mailing list