爲了好玩/練習python,我試圖創建一個顯示模塊內容的程序。看起來是這樣的:可變參數的Python dir()函數
import sys
print "Here are all the functions in the sys module: "
print dir(sys)
function = raw_input("Pick a function to see: ")
cnt = True
while cnt:
if function in dir(sys):
print "You chose:", function
cnt = False
else:
print "Invalid choice, please select again."
cnt = True
print dir("sys." + function)
但每一次,不管什麼字符串變量function
被設定爲,在dir("sys." + function)
電話始終默認爲相同的輸出dir(string)
(或因此我認爲!)
是什麼發生,有沒有辦法讓我做到這一點,並得到我真正想要的輸出(例如,變量function
設置爲stdin
,我得到的輸出爲dir(sys.stdin)
)?
使用'while cnt:'而不是'while cnt == True:' – jfs 2013-02-12 14:44:54