exit 2 levels of if/else and execute common code

Dan Sommers 2QdxY4RzWzUUiLuE at potatochowder.com
Mon Feb 11 10:45:09 EST 2019

On 2/11/19 9:25 AM, Neal Becker wrote:
> I have code with structure:
> ```
> if cond1:
>    [some code]
>    if cond2: #where cond2 depends on the above [some code]
>      [ more code]
>    else:
>      [ do xxyy ]
> else:
>    [ do the same xxyy as above ]
> ```
> So what's the best style to handle this?  As coded, it violates DRY.
> Try/except could be used with a custom exception, but that seems a bit heavy
> handed.  Suggestions?

Put some code, cond2, more code, and xxyy into separate
functions, and leave it the way it is.  We all have our own
levels of acceptable inelegance, and this one fits well
within mine.  Optionally, instead of separate calls to xxyy,
set a flag and  only call xxyy at the end if the flag is set.


More information about the Python-list mailing list