Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a function returns false in Python

In python, I am currently doing this:

if user_can_read(request.user, b) == False: 

Is there any other way of checking if the function returns False?

like image 555
q3d Avatar asked Aug 23 '12 18:08

q3d


People also ask

How do you check if a function is false in Python?

If you want to check that a variable is explicitly True or False (and is not truthy/falsy), use is ( if variable is True ). If you want to check if a variable is equal to 0 or if a list is empty, use if variable == 0 or if variable == [] .

How do I know if a Python return is true?

If user_can_read returns anything (except 0, False, etc), it will be considered True, and do stuff.

How do you know if a return is true or false?

Use the IF function along with AND, OR and NOT to perform multiple evaluations if conditions are True or False. The condition you want to test. The value that you want returned if the result of logical_test is TRUE. The value that you want returned if the result of logical_test is FALSE.

Can a function return true or false Python?

Python bool() function is used to return or convert a value to a Boolean value i.e., True or False, using the standard truth testing procedure.


1 Answers

You could just use

if user_can_read(request.user, b):     ## do stuff 

If user_can_read returns anything (except 0, False, etc), it will be considered True, and do stuff.

And the negation: if not user_can_read(request.user, b)

like image 128
John Avatar answered Sep 20 '22 18:09

John