Python基本菜单,创建了一个"loop"。这不好吗?



我用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)
通过从函数functionthatdoessomething()指向mainUI(),我们创建了一个"循环"。我们从mainUI()开始,我们做了一些事情,现在我们回到mainUI()。我不确定这是否是一个问题,但是通过这样做,yesOrNoMenu永远不会退出,我们在第一个yesOrNoMenu调用中保持"嵌套函数调用"。通过这样做,堆栈跟踪可以变得相当大。
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完成,它应该自然地重复自己,即使你没有显式地调用它。

相关内容

最新更新