ruk·si

🐍 Python
Data Types

Updated at 2017-01-02 23:22

Python does not have null, but None. None is a singleton object without behavior.

database_connection = None

try:
    database = MyDatabase(db_host, db_user, db_password, db_database)
    database_connection = database.connect()
except DatabaseException:
    pass

Never check for None with == syntax, use is. Classes may overwrite __eq__ operator with an implementation that does not take into account other being None.

if database_connection is None:
    print('The database could not connect')
else:
    print('The database could connect')

Sources