[Python-ideas] If branch merging

Andrew Barnert abarnert at yahoo.com
Sun Jun 7 06:29:35 CEST 2015

On Jun 6, 2015, at 20:03, Cory Beutler <cgbeutler at gmail.com> wrote:
> I recently(1 year ago) realized that 'if', 'elif', and 'else' provide easy branching for code, but there is no easy way to merge branches of code back together. One fix for this would be introduction of two new keywords: 'also' and 'alif' (also if)

Can you provide a realistic use case for when you'd want this, instead of just a toy example where you check meaningless variables for equality?

Because in practice, almost every time I've wanted complicated elif chains, deeply nested ifs, or anything else like it, it's been easy to either refactor the code into a function, replace the conditionals with a dict, or both. That isn't _always_ true, but I'm having a hard time coming up with an example where I really do need complicated elif chains and your new syntax would help.

> on True:
>     1. Execute code block
>     2. Jump to next 'alif', 'also', or end of if-chain
> on False:
>     1. jump to next 'elif', 'else', or end of if-chain
> Note: 'also' blocks can be useful in more places than just the end of a chain:
> if a == b:
>     print ('a == b')
> elif a == c:
>     print ('a == c')
> also:
>     print ('a == b == c')
> else:
>     print ('a != b and a != c')

This example seems to do the wrong thing. If a=b=2 and c=3, or a=c=3 and b=2, you're going to print "a == b == c" even though that isn't true. 

This implies that maybe it isn't as easy to think through the logic and keep the conditions in your head as you expected, even in relatively simple cases.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150606/9467e8e6/attachment.html>

More information about the Python-ideas mailing list