python 3中的None和False有什么区别?(在布尔意义上) [英] What is the difference between None and False in python 3? (in a boolean sense)

查看:66
本文介绍了python 3中的None和False有什么区别?(在布尔意义上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时python似乎将它们视为相同,而其他时候当使用False时它返回False但不返回任何内容.在其他语言中,它与 null 的行为似乎非常不同.

Sometimes python seems to treat them as the same whereas other times it returns False when False is used but returns nothing with None. It seems to behave very differently to null in other languages.

一些具体的例子是:

True 和 None 不返回任何内容False 和 None 返回 False

True and None returns nothing False and None returns False

推荐答案

Python 中的不同值可以用真实"来形容;或假的"即使它们不是 Boolean 值,这意味着它们在需要 Boolean<的情况下被解释为 TrueFalse/code> 值(例如 if 条件).如文档 中所定义,Python 中的每个值,无论类型如何,都被解释为 True except 对于以下值(被解释为 False):

Different values in Python can be described as being "truthy" or "falsy" even if they aren't Boolean values, which means they are interpreted as True or False in a situation that expects a Boolean value (such as an if condition). As defined in the documentation, every value in Python, regardless of type, is interpreted as being True except for the following values (which are interpreted as False):

  • 定义为假的常量:NoneFalse.
  • 任何数字类型的零:00.00jDecimal(0)分数(0, 1)
  • 空序列和集合:''()[]{}set(), range(0)
  • Constants defined to be false: None and False.
  • Zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
  • Empty sequences and collections: '', (), [], {}, set(), range(0)

针对你的具体情况,使用if情况,声明如下:

To your specific situation, using the if situation, the following statement:

if None:
    # some code here

在功能上等同于:

if False:
    # some code here

这是因为,如上表所示,为了 if 条件,值 None 会自动转换为 False.这就是所谓的语法糖",它是语言的一个特性,旨在让开发者的生活更轻松.

This is because, as shown in the list above, the value None is automatically converted to False for the purposes of the if condition. This is something referred to as "syntactic sugar", which is a feature of the language that exists to make the developer's life easier.

然而,仅仅因为 None 在这个特定场景中被解释False,这并不意味着这两个值等于每个其他.这是因为 False 是 True/False 对的一部分,指示二进制概念,如是/否"、开/关"等.,另一方面,代表了什么都没有的概念.值为 None 的变量意味着它们根本没有值.以比喻的形式将其与 False 进行比较,False 就像回答某人说No",其中 None 将是就像根本不回答他们一样.

However, just because None is interpreted as False in this particular scenario, that doesn't mean the two values are equal to each other. This is because False is meant to be part of the True/False pair indicating binary concepts like "yes/no", "on/off", etc. None, on the other hand, represents the concept of nothing. Variables with a value of None means they have no value at all. To compare it to False in the form of a metaphor, False would be like answering somebody by saying "No", where None would be like not answering them at all.

作为一个更实际的例子,请看下面的代码片段:

As a more practical example, see the following code snippet:

if None == False:
    # code in here would not execute because None is not equal to False

这篇关于python 3中的None和False有什么区别?(在布尔意义上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆