Jul-21-2018, 10:27 PM
i have a big long elif construct like
this is the kind of thing i seriously want to avoid
if condition-0: ... elif condition-1: ... elif condition-2: ... elif condition-3: ... elif condition-4: ... elif condition-5: ... elif condition-6: ... elif condition-7: ... elif condition-8: ... else: ... ...# a lot of ending codebut there are a few times i would like to shorten it and only do part of it like
if condition-0: ... elif condition-1: ... elif condition-2: ... elif condition-3: ... elif condition-4: ... elif condition-5: ... else: ... ...# a lot of ending codei am wanting to avoid replicating code so that the execution clauses, which are rather complex code, do not have to be duplicated by having both elif constructs. but i cannot, as far as i know, just wrap part of this in another if construct, such as the following invalid code
if condition-0: ... elif condition-1: ... elif condition-2: ... elif condition-3: ... elif condition-4: ... elif condition-5: ... if do-3-more-tests: elif condition-6: ... elif condition-7: ... elif condition-8: ... else: ... ...# a lot of ending codethe code in the else block is the same in both situations, whether do-3-more-tests is true or false. anyone have an idea how to do this (and keep the code from enlarging very much and avoid any duplications)?
this is the kind of thing i seriously want to avoid
if do-3-more-tests: if condition-0: ... elif condition-1: ... elif condition-2: ... elif condition-3: ... elif condition-4: ... elif condition-5: ... elif condition-6: ... elif condition-7: ... elif condition-8: ... else: ... else: if condition-0: ...# same as on line 3 above elif condition-1: ...# same as on line 5 above elif condition-2: ...# same as on line 7 above elif condition-3: ...# same as on line 9 above elif condition-4: ...# same as on line 11 above elif condition-5: ...# same as on line 13 above else: ...# same as on line 21 above ...# a lot of ending codesee the problem? i can see a way to do this by using a stack of if constructs that end with continue, by having it all in a loop. but there is code to be run at the end that would have to be duplicated 7 times. anyone have an idea how to do this (and keep the code from enlarging very much and avoid any duplications)?