我用python编写了一个基本的用户界面,但我有一个问题。代码如下:
def mainUI():
options=["doSomething","exit"]
index=0
print options[index]
command=raw_input("Give a command: ")
while True:
if command="select":
print "you selected ",options[index]
elif command="up":
index=UIListIndexIncrease(index,len(options))
print options[index]
command=raw_input("Give a command: ")
elif command="down":
index=UIListIndexDecrease(index)
print options[index]
command=raw_input("Give a command: ")
time.sleep(0.25)
def mainDispatch(index):
if index==0:
yesOrNoMenu(aFunctionThatDoesSomething)
elif index==1:
exitProgram()
def aFunctionThatDoesSomething():
...
mainUI()
def yesOrNoMenu(function,*opargs): #<------ this function never exits
index=0
options=["are you sure? \n [no] yes","are you sure? \n no [yes]"]
print options[index]
x=raw_input("give a command: ")
while True:
if x=="select":
dispatchYesNo(index,function,*opargs) #<--------- the function is passed to the dispatch function and is executed there, the function returns to MainUI().
#Then it goes from the dispatch back to the main function
break # <--------------------
if x=="up":
index=UIListIndexDecrease(index)
print options[index]
x=raw_input("give a command: ")
if x=="down":
index=UIListIndexIncrease(index,len(options))
print options[index]
x=raw_input("give a command: ")
time.sleep(0.25)
# all the code here is executed at the end, when the script ends
def dispatchYesNo(index,function,*opargs):
if index == 0:
mainUI()
elif index == 1:
function(*opargs)
通过从函数afunctionthatsomething()指向mainUI(),我们创建了一个“循环”。我们从mainUI()开始,做了一些事情,现在又回到mainUI()。 我不确定这是否是一个问题,但是这样做,yesOrNoMenu永远不会退出,我们在第一个yesOrNoMenu调用中继续“嵌套函数调用”。 通过这样做,stacktrace可以变得相当大。你知道吗
File "./transfer.py", line 383, in <module>
main()
File "./transfer.py", line 369, in main
mainUI()
File "./transfer.py", line 240, in mainUI
mainDispatch(index)
File "./transfer.py", line 227, in mainDispatch
aFunctionThatDoesSomething()
File "./transfer.py", line 286, in aFunctionThatDoesSomething
yesOrNoMenu(aFunctionThatDoesSomething)
File "./transfer.py", line 310, in yesOrNoMenu
dispatchYesNo(index,function,*opargs)
File "./transfer.py", line 324, in dispatchYesNo
mainUI()
File "./transfer.py", line 240, in mainUI
mainDispatch(index)
File "./transfer.py", line 227, in mainDispatch
aFunctionThatDoesSomething()
File "./transfer.py", line 286, in aFunctionThatDoesSomething
yesOrNoMenu(aFunctionThatDoesSomething)
File "./transfer.py", line 310, in yesOrNoMenu
dispatchYesNo(index,function,*opargs)
File "./transfer.py", line 324, in dispatchYesNo
mainUI()
File "./transfer.py", line 240, in mainUI
mainDispatch(index)
File "./transfer.py", line 227, in mainDispatch
aFunctionThatDoesSomething()
File "./transfer.py", line 286, in aFunctionThatDoesSomething
yesOrNoMenu(aFunctionThatDoesSomething)
File "./transfer.py", line 310, in yesOrNoMenu
dispatchYesNo(index,function,*opargs)
File "./transfer.py", line 324, in dispatchYesNo
mainUI()
File "./transfer.py", line 240, in mainUI
我不知道这是坏代码还是正常。有人能帮我吗? 谢谢
是的,这可能是不好的做法。Python有一个最大的递归限制;一旦堆栈跟踪有1000个函数,程序就会因
RuntimeError
而崩溃。你知道吗我建议修改
aFunctionThatDoesSomething
,这样它就不会调用mainUI
。不管怎样,main函数有一个while True:
循环,因此一旦aFunction
完成,它就应该自然地重复自己,即使您没有显式地调用它。你知道吗相关问题 更多 >
编程相关推荐