What is the null or None Keyword
The null
keyword is commonly used in many programming languages, such as Java, C++, C# and Javascript. It is a value that is assigned to a variable. Perhaps you have seen something like this:
null in Javascript
null in PHP
null in Java
The concept of a null
keyword is that it gives a variable a neutral, or "null" behaviour. Note that technically the behaviour of null
changes between higher and lower-level languages, so to keeps things simple we'll be referring to the concept in object-orientated languages.
Python's null Equivalent: None
The equivalent of the null
keyword in Python is None
. It was designed this way for two reasons:
- Many would argue that the word "null" is somewhat esoteric. It's not exactly the most friendliest word to programming novices. Also, "None" refers exactly to the intended functionality - it is nothing, and has no behaviour.
- In most object-oriented languages, the naming of objects tend to use camel-case syntax. eg.
ThisIsMyObject
. As you'll see soon, Python'sNone
type is an object, and behaves as one.
The syntax to assign the None
type to a variable, is very simple. As follows:
Why Use Python's None Type?
There are many cases on why you would use None
.
Often you will want to perform an action that may or may not work. Using None
is one way you can check the state of the action later. Here's an example:
Another scenario would be where you may need to instantiate a class, depending on a condition. You could assign a variable to None
, then optionally later assign it to an object instance. Then after that you may need to check if the class has been instantiated. There are countless examples - feel free to provide some in the comments!
Python's None is Object-Orientated
Python is very object-orientated, and you'll soon see why. Note that the Null
keyword is an object, and behaves as one. If we check what type the None
object is, we get the following:
We can discover three things from this:
-
None
is an object - a class. Not a basic type, such as digits, orTrue
andFalse
. - In Python 3.x, the
type
object was changed to new style classes. However the behaviour ofNone
is the same. - Because
None
is an object, we cannot use it to check if a variable exists. It is a value/object, not an operator used to check a condition.
Checking if a Variable is None
There are two ways to check if a variable is None
. One way can be performed by using the is
keyword. Another is using the ==
syntax. Both comparison methods are different, and you'll see why later:
This code will give us the following output:
Great, so they're the same! Well, sort of. With basic types they are. However with classes you need to be careful. Python provides classes/objects with the ability to override comparison operators. So you can compare classes, for example MyObject == MyOtherObject
. This article won't go into depth on how to override comparison operators in classes, but it should provide insight as to why you should avoid checking if a variable is None
using the ==
syntax.
That gives us the following output:
Interesting! So you can see that the is
keyword checks if two objects are exactly the same. Whereas the ==
operator first checks if the class has overridden the operator. For the PHP coders out there, using the ==
syntax is the same as ==
in Python, where using the is
keyword is equivalent to the ===
syntax.
Because of this, it's always advisable to use the is
keyword to check if two variables are exactly the same