2016-11-22 67 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.