博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
None Python
阅读量:4032 次
发布时间:2019-05-24

本文共 5664 字,大约阅读时间需要 18 分钟。

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'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:

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, or True and False.
  • In Python 3.x, the type object was changed to new style classes. However the behaviour of None 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

转载地址:http://ojhbi.baihongyu.com/

你可能感兴趣的文章
python一句话之利用文件对话框获取文件路径
查看>>
PaperDownloader——文献命名6起来
查看>>
PaperDownloader 1.5.1——更加人性化的文献下载命名解决方案
查看>>
如何将PaperDownloader下载的文献存放到任意位置
查看>>
C/C++中关于动态生成一维数组和二维数组的学习
查看>>
系统架构:Web应用架构的新趋势---前端和后端分离的一点想法
查看>>
JVM最简生存指南
查看>>
漂亮的代码,糟糕的行为——解决Java运行时的内存问题
查看>>
Java的对象驻留
查看>>
自己动手写GC
查看>>
Java 8新特性终极指南
查看>>
logback高级特性使用(二) 自定义Pattern模板
查看>>
JVM并发机制探讨—内存模型、内存可见性和指令重排序
查看>>
可扩展、高可用服务网络设计方案
查看>>
如何构建高扩展性网站
查看>>
微服务架构的设计模式
查看>>
持续可用与CAP理论 – 一个系统开发者的观点
查看>>
nginx+tomcat+memcached (msm)实现 session同步复制
查看>>
c++指针常量与常量指针详解
查看>>
c++字符数组和字符指针区别以及str***函数
查看>>