what is the correct way to check for False?

patchwork

Which is better? (and why?)

if somevalue == False:

or

if somevalue is False:

Does your answer change if somevalue is a string?

MSeifert

It rather depends on what somevalue can be: if somevalue could be anything you could check that it's a boolean and not:

if isinstance(somevalue, bool) and not somevalue

this doesn't rely on False being a singleton. If it always is a singleton you can also do:

if somevalue is False

But PEP8 of Python states you shouldn't care if it about the class and just use:

if not somevalue

this will evaluate if somevalue is "falsy". See Python documentation on Truth value testing.

PEP8 states:

Don't compare boolean values to True or False using == .

and gives these examples:

Yes:   if greeting:
No:    if greeting == True:
Worse: if greeting is True:

which translates in your case to:

Yes:   if not greeting:
No:    if greeting == False:
Worse: if greeting is False:

Keep in mind that each string is considered "truthy" except the empty string ''.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What's the correct way to check if a collection exists?

What is the correct way to check url type value?

What is the correct way to check for a '?' in a case statement

What is the correct way to check if a global variable exists?

What is the correct way to check if a cell in a database is null

What is the correct way to check if CancellationToken was invoked for backgroundservice

What is the correct way to check for "possibly null" references?

Js) what will be the simplest way to check null or '' as false but not 0?

What is the correct way to check if current process/thread is the main process/thread?

what is the correct way to check variable is present in http response?

What is the correct way to check nil pointer de-reference?

What's the correct way to do a health check on a Twilio connection in TypeScript?

What is the best way to check correct dtypes in a pandas dataframe as part of testing?

What's the correct way to check if an object is a typing.Generic?

What is the correct way to check an object's type in a conditional statement - python

What is the correct way to check errors with firebase auth methods?

What is the correct/standard way to check if difference is smaller than machine precision?

What is the correct & idiomatic way to check if a string starts with a certain character in Rust?

What is the correct way to check for String equality in Typescript (for Angular 4)

What is the correct way to do this?

Correct way to check if a type is Nullable

Correct way to check if object is decimal

What is the proper way to manually check and uncheck a RadioButton when the AutoCheck property is set to false in C#?

What is the correct way to check object's property if object not undefined in angular 2 template?

What is the correct way to use for/fold check if an element exists in nested list ;Racket?

What is the correct way to cancel a RegNotifyChangeKeyValue?

What is the correct way to code in Go?

What is the correct way to use grids?

What is the correct way to use prepareForReuse?