[英]Suppress the terminal output of a Python command
try: var_sigma
except: print "Not defined."
This code prints the value of var_sigma
if it's defined, but I want it to print nothing at all. 如果已定义,此代码将打印
var_sigma
的值,但我希望它不打印任何内容。 How can I accomplish this? 我该怎么做?
Replace the print
with pass
: 用
pass
替换print
:
try: var_sigma
except: pass
Although it might be better to rethink your design. 尽管最好重新考虑您的设计。 What will happen if you then try to use the variable?
如果您随后尝试使用该变量,将会发生什么?
try:
print var_sigma
except:
print "Not defined."
The above would print var_sigma
if it were defined. 如果已定义,则上面将显示
var_sigma
。 But your code sample does not do that, it does nothing if it is defined, and if its not defined, it prints something. 但是您的代码示例不会这样做,如果定义了代码,它将不执行任何操作;如果未定义,则将执行某些操作。
If you simple wanted to check whether var_sigma
exists, then you could do the following: 如果您只是想检查
var_sigma
是否存在,则可以执行以下操作:
try:
assert isinstance(var_sigma, object)
except AssertionError:
pass
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.