2016-11-22 73 views
4

每次提示重新顯示時,讓Python交互式shell顯示當前的時間將會非常方便。我想設置我的提示類似於: sys.ps1 = str(datetime.datetime.now().time()。isoformat()[:8])在顯示提示之前評估Python Interactive Shell中的提示

由於不會每次評估提示它會顯示出來,這隻會顯示創建shell的時間,並且在其生命週期中不會更新。

我更頻繁地使用3.5.1版本 - 以防萬一。

有沒有辦法讓shell在每次顯示提示時在之前評估提示字符串

感謝您的回覆和時間。

回答

1
import sys 
import datetime 

class Prompt(): 
    def __str__(self): 
     return str(datetime.datetime.now().time().isoformat()[:8]) 

sys.ps1 = Prompt() 

https://docs.python.org/2/library/sys.html

If a non-string object is assigned to either variable, its str() 
is re-evaluated each time the interpreter prepares to read a 
new interactive command; this can be used to implement a 
dynamic prompt.