Reputation: 21
Is there a way to pass a function into the string portion of an 'assert' statement like below? If not, are there any keywords in which I can do so?
f=5
def something():
assert f!=5, do_something_else()
Upvotes: 0
Views: 95
Reputation: 392
assert
is used for Testing Warnings and you shouldn't rely on it for executing a block of codes.
official documentation
Upvotes: 1
Reputation: 978
You can put a function call in an assert
statement just as you showed. The function doesn't even have to return a string. Python will try to put out a readable representation of whatever the function returns, including None
.
The function will only be called if the condition after assert
is false.
Upvotes: 0
Reputation: 140
You can use an if statement for this:
f = 5
def something():
if f!= 5:
do_something_else()
Upvotes: 1