def fun(x):
for k in range(10):
found = False
if x < 12 and other(k):
dostuff()
found = True
if x == 4 and other2(k):
dostuff()
found = True
if not found:
dootherstuff(k)
I have this code. My question is if these if statements can be evaluated beforehand since x does not change?
The code should do the same thing as:
def fun(x):
if x == 4:
for k in range(10):
if other2(k):
dostuff()
else:
dootherstuff(k)
if x < 12:
for k in range(10):
if other(k):
dostuff()
else:
dootherstuff(k)
Or
def fun(x):
for k in range(10):
if x == 4 and other2(k) or x < 10 and other(k):
dostuff()
else:
dootherstuff(k)
But since both of these are very un DRY and ugly I wonder if there is a better option. In my real code I have way more statements but all I need is for some values of X a specific check in the loop and I don't want to check X in every iteration since it does not change.
Aucun commentaire:
Enregistrer un commentaire