[Python] Check for None (Null) in Python

本文介绍了如何在Python中区分空对象与不存在的对象,并详细解释了使用'is'与'=='来判断对象是否为None的区别及性能差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

If you’ve used other programming languages, you may have learned that an empty object is not the same as an object that does not exist. In this lesson, you’ll learn how to check for None (or Null objects) in Python.

 

foo = None

if foo is None:
    print("is None")

if foo == None:
    print("also none")

 

Using 'is' can be better when check is None or not, because 'is' is doing id comparsion:

id(foo) == id(None)

It is much faster check '==' it does a deep looking for the value.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值