[Python-ideas] except expression

Chris Angelico rosuav at gmail.com
Wed Feb 19 14:52:22 CET 2014


On Thu, Feb 20, 2014 at 12:39 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Wed, Feb 19, 2014 at 10:28:59PM +1000, Nick Coghlan wrote:
>
>> Either way, based on your survey of the stdlib, I agree with MAL's
>> suggestion to also rule out embedded name binding. The example you
>> give in the PEP of losing the distinction between whether an iterator
>> yielded a new incremental value or terminated and returned a final
>> value is another sign that allowing name binding is a potential bug
>> magnet, because it conflates two different return channels into one.
>
> Hmmm, I think that's a bit strong. If combining the iterator's yield
> value and the iterator's return value is a mistake, that's not the
> syntax that makes it a mistake, it's that the very idea of doing so is
> wrong.

And by the way, I'd say that it's wrong *in the general sense* to
conflate yield/return, but such conflations happen all the time. We
can use 'or' to turn any falsy value into some specific thing:

def foo(widget, n=None):
    """Frob the widget n times, or once for each spam"""
    for i in range(n or len(widget.spam)):
        widget.frob()

Nobody would ever suggest that 0, None, [], and "" should all be
treated the same everywhere, but pass any of them to this function and
it'll do the same thing.

By moving the conflation as far outward as possible, we maintain the
distinction as long as possible. At the point where the programmer
knows that the two channels must mean the same thing, they can get
merged. It's like merging stdout and stderr of a process prior to
giving the text to a human.

ChrisA


More information about the Python-ideas mailing list