[Python-ideas] If branch merging
Bruce Leban
bruce at leban.us
Sun Jun 7 07:50:52 CEST 2015
On Sat, Jun 6, 2015 at 8:03 PM, Cory Beutler <cgbeutler at gmail.com> wrote:
> *also *- execute code if any previous condition is met
> <snip>
> Thank you for humoring my idea. I am new to this mailing list, so sorry if
> this seems out of line or something.
>
Seeing many posts on this list which are repeats of ideas seen many times,
it's nice to see a new idea. I think the difficulty of making this work is
how often you want something only when *all* of the previous conditions are
true yet can't conveniently do it another way (e.g., setting a flag).
Your point about writing conditions multiple time is legitimate and happens
frequently. Here's an example of something where there is a similar
difficulty in writing simple code:
if foo.a == 0:
...
elif foo.a == 1 and foo.b == 0:
...
elif foo.a >= 1 and foo.b >= 0 and foo.c = 0:
...
elif ...
This is a generic example but I've written code like this many times and
there is no simple way to say that all the foo.x values don't need to be
computed more than once. Here it is rewritten to avoid recomputation:
foo_a = foo.a
if foo_a == 0:
...
else:
foo_b = foo.b
if foo_a == 1 and foo_b == 0:
...
else:
foo_c = foo.c
if foo_a >= 1 and foo_b >= 0 and foo_c = 0:
...
else:
...
Much harder to follow the logic. A simpler example where the same
recomputation happens is:
x = a and a.b and a.b.c and a.b.c.d
which becomes
x = a and a.b
if x: x = x.c
if x: x = x.d
Ick.
--- Bruce
Check out my new puzzle book: http://J.mp/ingToConclusions
Get it free here: http://J.mp/ingToConclusionsFree (available on iOS)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150606/f03c488c/attachment.html>
More information about the Python-ideas
mailing list