如何避免一直键入 运行() 的参数

How to avoid typing the arguments to run() all the time

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

# 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() ?

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

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)