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.
很明显,你应该让它充满活力!使用__str__方法创建一个对象,您可以在其中放置所需的任何逻辑:
class Prompt:
def __str__(self):
# Logic to randomly determine string
return string
试试这个:
根据docs,如果你给
sys.ps1
分配一个非字符串对象,那么它每次都会计算它的str
函数:很明显,你应该让它充满活力!使用
__str__
方法创建一个对象,您可以在其中放置所需的任何逻辑:您也可以在进行更改或在该类中插入内容。例如,您可以在
Prompt
中添加或更改消息列表,这将影响控制台消息。在对于更改提示,我们使用
现在随机的方法是这样的:
^{pr2}$要在python解释器启动时执行此操作,可以遵循以下指南:https://docs.python.org/2/tutorial/appendix.html#the-interactive-startup-file
相关问题 更多 >
编程相关推荐