![](/img/trans.png)
[英]Cannot understand why python doesn't re-use the parameter when you repeatedly call the same function
[英]How to re-use the same function but call it from different locations in python in same test script
我有一個測試用例,其中我必須從設備上的不同屏幕“結束進程”,並且具有模擬不同屏幕的功能。 EndProcess()之后,設備返回到screen1()。 有什么樂觀的方法可以在Python中做到這一點嗎? 我可以在這里使用發電機嗎?
目前,我的代碼是:
while 1:
screen1()
EndProcess()
screen1()
screen2()
EndProcess()
screen1()
screen2()
screen3()
EndProcess()
當屏幕數量變大時,您將重復很多次。 相反,您可以將屏幕放在列表中,並使用for循環調用它們:
screens = [screen1, screen2, screen3]
while True:
for x in range(len(screens)):
for i in range(x+1):
screens[i]()
EndProcess()
在Python 2中使用xrange
代替range
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.