Comparison if datetime.datetime or None exists

I am running a small application in Google App Engine with Python. In the model, I have a property of type DateTimeProperty, which is equal to datetime.datetime. When it is created, there is no value (ie, "No"). I want to compare if this datetime.datetime is None, but I cannot.

if object.updated_date is None or object.updated_date >= past:
  object.updated_date = now

Both updated_dateand pastare datetime.datetime.

I get the following error.

TypeError: cannot compare datetime.datetime with NoneType

What is the right way to do this?

+5
source share
3 answers

You want and, not or.

You can also use is None.

EDIT:

, object.updated_date None, , past None.

+3

, :

if object.updated_date and object.updated_date >= past:

( null), >= . , , , .

+5

, , , , None, ( object.updated_date, None):

if None in (past, object.updated_date) or object.updated_date >= past:
  object.updated_date = now

, None in (past, object.updated_date) , (past is None or object.update_date is None) (, , , , ).

, ;-), ( ..). object - - , . obj , (, ;-), . "" "" , ( , , ), ; , /... .

I understand that many of the names of the built-in Python modules are "attractive nuisance" in the sense of ... file, object, list, dict, set, min, max... everything visible attractive name for the "File", "object", "list", etc. But you should learn to resist this particular temptation! -)

+4
source

All Articles