lundi 2 octobre 2017

How to figure if a variable is None, False or True

Let's say we have the following code which detects the stars rating of a document. If the stars rating is 50.0 it'll indicate it using stars_indicator = True and we would like to 'do something' in such case, if the stars rating is 10.0 it'll indicate it using stars_indicator = False and we would like to 'do something else' in this case.

stars_indicator = sentiment_indicator = None
if document[stars] == 50.:
   stars_indicator = True
elif document[stars] == 10.:
   stars_indicator = False

How can we check if we should 'do something' or 'do something else'?

Checking if it's True is simple

if stars_indicator:
   # do something

The trivial approach for checking if it's False or None will be

if not stars_indicator:
   # do something else

But in this way the if condition won't distinguish between the two options and will 'do something else' if stars_indicator False or None.

Aucun commentaire:

Enregistrer un commentaire