mardi 10 août 2021

if else condition for with statement in Python [duplicate]

I have a condition that can be either True or False and depending on this condition I want to run a block of code inside a with statement or not, something like this:

if use_with:
    with bla_bla_bla:
        # Block of code
else:
    # Block of code

This does work, but it is a horrible code duplication prompt to problems in the future. What is the way of doing this?

Aucun commentaire:

Enregistrer un commentaire