本文共 5664 字,大约阅读时间需要 18 分钟。
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:
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.
The equivalent of the null
keyword in Python is None
. It was designed this way for two reasons:
ThisIsMyObject
. As you'll see soon, Python's None
type is an object, and behaves as one. The syntax to assign the None
type to a variable, is very simple. As follows:
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 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, or True
and False
.type
object was changed to new style classes. However the behaviour of None
is the same.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. 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
转载地址:http://ojhbi.baihongyu.com/