提出需求:由输入的参数决定程序开始执行的位置,并执行至结束
场景demo:一个包含了以下步骤的图像处理函数,
step0:相机采图
step1:灰度转换
step2:二值转换
step3:查找轮廓
step4:计数运算
假设已经运行过一次上述的图像处理函数,但是对结果不满意,对二值转换的参数进行了微调,那么就只需要从step2开始再执行一次即可,而无需从step0开始。
以下是几种实现的方法:
使用判断语句
def function(step):
if step == 0:
print('step0 do something')
step += 1
if step == 1:
print('step1 do something')
step += 1
if step == 2:
print('step2 do something')
step += 1
if step == 3:
print('step3 do something')
step += 1
if step == 4:
print('step4 do something')
step += 1
if step == 5:
print('step5 do something'