擅长:python、mysql、java
<p>根据<a href="https://docs.python.org/2/library/sys.html?highlight=sys#sys.ps1">docs</a>,如果你给<code>sys.ps1</code>分配一个非字符串对象,那么它每次都会计算它的<code>str</code>函数:</p>
<blockquote>
<p>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.</p>
</blockquote>
<p>很明显,你应该让它充满活力!使用<code>__str__</code>方法创建一个对象,您可以在其中放置所需的任何逻辑:</p>
<pre><code>class Prompt:
def __str__(self):
# Logic to randomly determine string
return string
</code></pre>
<p>您也可以在进行更改或在该类中插入内容。例如,您可以在<code>Prompt</code>中添加或更改消息列表,这将影响控制台消息。在</p>