None对象的特殊性

  1. 和C、Java不同,Python中没有NULL,取而代之的是None
  2. None是一个特殊的常量,表示变量没有指向任何对象
  3. 在Python中,None本身实际上也是对象,有自己的类型NoneType
  4. 可以将None赋值给任何变量,但我们不能创建NoneType类型的对象
var = None
var2 = None
print(type(None))
print(id(None))
print(id(var))
print(id(var2))

"""
<class 'NoneType'>
140708184136920
140708184136920
140708184136920
"""

None不是False,None不是0,None不是空字符串。None和任何其他的数据类型比较永远返回False

None和其他类型的比较

a = []
b = ()
c = {}
d = ""
e = 0
f = None

if (not a) and (not b) and (not c) and (not d) and (not e) and (not f):
    print("if判断时,空列表、空字符串、0、None等代表空和无的对象会被转换成False")
a = []
b = ()
c = {}
d = ""
e = 0


if (a==False or d==False or b==False or d==False):
    print("==时,空列表、空字符串不是False")

if (e==False):
    print("==时,0会转成False")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值