Book Image

Python Essentials

By : Steven F. Lott
Book Image

Python Essentials

By: Steven F. Lott

Overview of this book

Table of Contents (22 chapters)
Python Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

The logic of the None object


In Chapter 2, Simple Data Types, we introduced the None object. It is a unique, immutable object, often used to indicate that a parameter should have a default value or that an input is not available. Some languages have a special null object or null pointer that have similar semantics to the Python None object.

The None object has no arithmetic operators defined. It's equivalent to False. The == and != operators are generally defined for None. However, these operators aren't always appropriate because other objects might exhibit similar behavior.

Generally, we'll use the is comparison when trying to determine if a variable is set to None. The == test can be redefined by a class that implements the __eq__ special method; the is test cannot be overridden.

Tip

Because == can be reimplemented, always use is None instead of == None.

Since bool(None) == False, we can use a variable which may be None in an if condition. Nevertheless, we should generally use is None or is...