I often find myself coding structures similar to the following:
if x:
if y:
#Do thing 0
else:
#Do thing 1
else:
if y:
#Do thing 2
else:
#Do thing 3
Sometimes with more levels of nesting, or 3 conditions per variable, etc. I'd like to. clean up the code, so as to not repeat if y:. I've thought of creating an array of functions thing1, thing2 so on, and then indexing with f[x, y], however there is often quite different behaviour, which makes this hard. For example, in a class, thing1 might use two variables and modify another 2, while thing2 might do something entirely different with different variables (this makes returning the results in a uniform way hard).
Aucun commentaire:
Enregistrer un commentaire