brastein
brastein

Reputation: 77

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. How can I accomplish this?

Upvotes: 1

Views: 254

Answers (2)

Nafiul Islam
Nafiul Islam

Reputation: 82550

try:
    print var_sigma
except:
    print "Not defined."

The above would print var_sigma if it were defined. 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:

try:
    assert isinstance(var_sigma, object)
except AssertionError:
    pass

Upvotes: 1

cdarke
cdarke

Reputation: 44394

Replace the print with pass:

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?

Upvotes: 4

Related Questions