在显示提示之前评估Python Interactive Shell中的Promt

时间:2016-11-22 00:57:35

标签: python

每次重新显示提示时,让Python交互式shell显示当前的时间非常方便。我正在考虑将我的提示设置为:     sys.ps1 = str(datetime.datetime.now()。time()。isoformat()[:8])

由于每次显示时都不会评估提示,因此这只会显示创建shell时的时间,并且在生命周期内不会更新。

我更频繁地使用3.5.1版本 - 如果重要的话。

有没有办法让shell在每次显示提示时之前评估提示字符串

感谢您的回复和时间。

1 个答案:

答案 0 :(得分: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.