I was just thinking of something that would be really cool to have in my if-elif-else controls.
if condition:
stuff()
elif condition:
otherstuff()
then:
stuff_that_applies_to_both()
else:
stuff_that_doesnt_aply_to_either()
So basically a then will be run when any of the conditions are run EXCEPT the else condition. Do you think this is useful? It's similar to the try-except-else of python.
I think some of you are nitpicking a very preliminary implementation. The then block would be just like the else block in a try-except block in python. The real reason I suggest this is for situations like this.
m = {}
if condition == '1':
m['condition'] = condition
elif condition2 == '3':
m['condition2'] = condition2
elif condition3 == 'False':
m['condition3'] = True
then:
run_test_that_relies_on_one_of_the_conditions_being_true()
return m
The then block is scoped to the first if just like the else is. So nesting works fine. And if you need to run a method before the if statements, that really has nothing to do with this use case.
finallyin Java? – Alex Feinman Oct 19 '10 at 19:15thento be a bit confusing. Usuallythenis implied to occur after anif. I mean, you are sayingif condition, then stuff()but then proceed to saythen stuff that applies to both– Matt Olenik Oct 19 '10 at 19:27