[Python-ideas] if expensive_computation() as x:

Chris Angelico rosuav at gmail.com
Sat Feb 15 02:43:36 CET 2014


On Sat, Feb 15, 2014 at 12:40 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> If the handling is identical, there's no need for an if/elif chain at
> all, otherwise the revised if/elif chain is based on the attributes of
> the match object.

If the handling's identical, short-circuiting 'or' will do the job.
Assume it's not.

>> Removing one is just deleting its elif and corresponding block of
>> code. They are peers. The fact that Python doesn't currently have
>> syntax that allows this *in an elif chain* doesn't change this; if you
>> were to write it as pseudo-code, you would write them as peers. That's
>> why the function-with-return or loop-with-break still looks better,
>> because it structures the code the way that logically makes sense -
>> flat, not nested.
>
> That doesn't mean embedded assignments are the answer though - they're
> a sledgehammer solution that is tempting as a workaround for larger
> structural issues.

I agree that embedded assignment isn't necessarily the answer. I just
think that a solution that has them all at the same indentation level
makes more sense than one that nests them inside each other, for
exactly the same reason that we have 'elif' in the first place.

ChrisA


More information about the Python-ideas mailing list