I have a five or six resources that have nice 'with' handlers, and normally I'd do this:
with res1, res2, res3, res4, res5, res6:
do1
do2
However, sometimes one or more of these resources should not be activated. Which leads to very ugly repetitive code:
with res1, res3, res4, res6: # these always acquired
if res2_enabled:
with res2:
if res5_enabled:
with res5:
do1
do2
else:
do1
do2
else if res5_enabled:
with res5:
...
There must be clean easy ways to do this surely?
with res_list:and (if necessary) iterating through theres_listinside the with statement? Since I'm not sure what you're doing or how it uses the resources, I can't provide a much better answer... Are the individual resources passed as function parameters? You might try a dictionary if the name is important, too, such asres_dict = {}; res_dict['res1'] = <res1>; res_dict['res2'] = <res2>; ....