Reputation: 5381
How do I run multiple commands after calling an assert
statement? For example, here's what I would like to do (without using assert):
x = False
if x != True:
my_func()
raise ValueError("My statement")
This does exactly what I want, but it seems more Pythonic to use assert
in this case. I cannot figure out how to do multiple things after calling assert
. Here's what I'm trying to do (but with incorrect syntax):
x = False
assert x == True, my_func() "My statement"
Upvotes: 1
Views: 605
Reputation: 24562
You could do
assert x == True, [my_func(), "My statement"][1]
DEMO
def my_func():
print("my function")
x = False
assert x == True, [my_func(), "My statement"][1]
OUTPUT
my function
Traceback (most recent call last):
File "C:/Users/abdul.niyas/AppData/Local/Programs/Python/Python36-32/a.py", line 5, in <module>
assert x == True, [my_func(), "My statement"][1]
AssertionError: My statement
Upvotes: 1