In Python 3.8, you will be able to write
if error := dict.get("error", None):
print(error)
Python does not have Perl-style statement modifiers, but since print
is a function, you can write an expression statement like
print(error) if (error := dict.get("error", None)) else None
although I wouldn't recommend it. (Actually, I'm not entirely sure the assignment expressions defined by PEP-572 interact with a conditional expression. I haven't played with any reference implementations to test it, but I believe error
will be in scope for the entire expression.)
A simple if
statement like that, though, can be written on one line (although again, I wouldn't recommend it; PEP-8 explicitly frowns upon
if error := dict.get('error', None): print(error)
)