exit 2 levels of if/else and execute common code

Rhodri James rhodri at kynesim.co.uk
Mon Feb 11 10:38:26 EST 2019


On 11/02/2019 15:25, 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?

If it's trivial, ignore DRY.  That's making work for the sake of making 
work in such a situation.

If it isn't trivial, is there any reason not to put the common code in a 
function?

-- 
Rhodri James *-* Kynesim Ltd


More information about the Python-list mailing list