python中if x 和if x is not None的区别用法详解

注意:以下两种写法是有区别的。

  • if x 和 if x is not None
  • if not x 和 if x is None

        python里面的其他值也被检测为False。最常见的例子就是空列表bool([])也返回False。但是,空列表有一个隐含的意思,它不等于None!!None意味着没有值,而空列表意味着零个值,这在语义上是不同的!

示例代码1:

x = None

print('test1' + '-' * 50)
if x:
    print('if x')

print('test2' + '-' * 50)
if x is not None:
    print('if x is not None')

print('test3' + '-' * 50)
if not x:
    print('if x')

print('test4' + '-' * 50)
if x is None:
    print('if x is not None')

print('test5' + '-' * 50)
print(bool(x))

运行结果:

 示例代码2:  【空列表不等于None】【python会自动给非boolean类型进行bool判断的时候转换成boolean类型

x = []

print('test1' + '-' * 50)
if x:
    print('if x')

print('test2' + '-' * 50)
if x is not None:
    print('if x is not None')

print('test3' + '-' * 50)
if not x:
    print('if x')

print('test4' + '-' * 50)
if x is None:
    print('if x is not None')

print('test5' + '-' * 50)
print(bool(x))

运行结果:

示例代码3:

# x = [0]
x = [1]  # 这儿写0和1是一样的

print('test1' + '-' * 50)
if x:
    print('if x')

print('test2' + '-' * 50)
if x is not None:
    print('if x is not None')

print('test3' + '-' * 50)
if not x:
    print('if x')

print('test4' + '-' * 50)
if x is None:
    print('if x is not None')

print('test5' + '-' * 50)
print(bool(x))

运行结果:

 示例代码4:

x = 0

print('test1' + '-' * 50)
if x:
    print('if x')

print('test2' + '-' * 50)
if x is not None:
    print('if x is not None')

print('test3' + '-' * 50)
if not x:
    print('if x')

print('test4' + '-' * 50)
if x is None:
    print('if x is not None')

print('test5' + '-' * 50)
print(bool(x))

运行结果:

示例代码5:

x = 1

print('test1' + '-' * 50)
if x:
    print('if x')

print('test2' + '-' * 50)
if x is not None:
    print('if x is not None')

print('test3' + '-' * 50)
if not x:
    print('if x')

print('test4' + '-' * 50)
if x is None:
    print('if x is not None')

print('test5' + '-' * 50)
print(bool(x))

运行结果:

示例代码6:  【自定义类】【x是被__bool__初始化的】

class Foo(object):
    def __bool__(self):
        print("Foo将会被检测为Boolean类型")
        return True


x = Foo()

print('test1' + '-' * 50)
if x:
    print('if x')

print('test2' + '-' * 50)
if x is not None:
    print('if x is not None')

print('test3' + '-' * 50)
if not x:
    print('if x')

print('test4' + '-' * 50)
if x is None:
    print('if x is not None')

print('test5' + '-' * 50)
print(bool(x))

运行结果:

示例代码7:

class Foo(object):
    def __bool__(self):
        print("Foo将会被检测为Boolean类型")
        return False


x = Foo()

print('test1' + '-' * 50)
if x:
    print('if x')

print('test2' + '-' * 50)
if x is not None:
    print('if x is not None')

print('test3' + '-' * 50)
if not x:
    print('if x')

print('test4' + '-' * 50)
if x is None:
    print('if x is not None')

print('test5' + '-' * 50)
print(bool(x))

运行结果:

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值