如何避免一直输入run()的参数

时间:2015-01-14 05:30:51

标签: python vlab

在我的VLAB python脚本中,我反复为time_unit和blocking输入相同的参数:

# nothing for a while
write_port("pwm_0.period", 0)
run(200, "ns", blocking=True)

# start a waveform  
write_port("pwm_0.period", 100)

# see it operate for 2 cycle) 
run(230, "ns", blocking=True)

# change duty cycle
write_port("pwm_0.duty", 10)
run(200, "ns", blocking=True)

# change period
write_port("pwm_0.period", 50)
run(200, "ns", blocking=True)

有什么方法可以避免输入

"ns", blocking=True

每次拨打run()

1 个答案:

答案 0 :(得分:3)

是的,您可以定义一个将指定值作为输入的函数,然后添加默认后缀:

def my_run(num):
    run(num, "ns", blocking=True)

现在,代替上面的代码,这将成为:

# nothing for a while
write_port("pwm_0.period", 0)
my_run(200)

# start a waveform  
write_port("pwm_0.period", 100)

# see it operate for 2 cycle) 
my_run(230)

# change duty cycle
write_port("pwm_0.duty", 10)
my_run(200)

# change period
write_port("pwm_0.period", 50)
my_run(200)