I have many functions that sometimes should loop sometimes shouldn't. I want to build in an option to tell it to loop. Here is my code. You can see that every function has the if... else statement. Is there a way to put the if... else statement into the method loop? So I don't have to repeat those lines for every function?
import inspect
def test1(b,shouldloop='no'):
if shouldloop =='no':
a = b + 1
print a
else:
loop(lambda z: test1(z) , inspect.currentframe().f_code.co_name)
def test2(d,e,shouldloop='no'):
if shouldloop =='no':
a = d * e
print a
else:
loop(lambda z: test2(z,e) , inspect.currentframe().f_code.co_name)
def test3(g,h,i,shouldloop='no'):
if shouldloop =='no':
a = g **2 - h + i
print a
else:
loop(lambda z: test3(z,h,i) , inspect.currentframe().f_code.co_name)
def loop(function,c):
x = [1,2,3,4]
for i in x:
function (i)
print c
test1(2,'yes')
test2(2,5,'yes')
test3(2,5,4,'yes')
Aucun commentaire:
Enregistrer un commentaire