擅长:python、mysql、java
<p>不,这在你描述的方式中是不可能的,但是我猜你想在交互式shell中使用这种语法,否则我甚至无法想象这有多有用。在本例中,使用<a href="https://docs.python.org/2/library/cmd.html" rel="nofollow">cmd</a>模块编写自己的shell将是一个不错的选择。例如:</p>
<pre><code>import cmd
class SpecialPrint(cmd.Cmd):
def do_print(self, line):
print line
def do_exit(self, line):
return True
if __name__ == '__main__':
SpecialPrint().cmdloop()
</code></pre>
<p>运行此代码将生成一个shell,其工作方式如下:</p>
<pre><code>(Cmd) print !
!
(Cmd) print anything you want
anything you want
(Cmd) exit
</code></pre>