了解python字符串的真实性 [英] Understanding truthiness of python strings

查看:153
本文介绍了了解python字符串的真实性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解Python内置类型具有真实性"值,空字符串被视为False,而任何非空字符串均被视为True.

这很有意义

我可以使用内置功能bool进行检查.

>>> bool("")
False

>>> bool("dog")
True

在使用条件语句时,我也可以利用这些真实性值.例如:

>>> if "dog":
...     print("yes")
...
yes

这令人困惑

这对==运算符不起作用:

>>> "dog" == True
False

>>> "dog" == False
False

任何人都可以解释为什么==似乎与有条件的行为有所不同吗?

解决方案

请参见比较文档的各个部分,摘录如下.

简而言之,大多数情况默认情况下都是真实的,这就是bool("dog")为真的原因. ==运算符比较两个对象是否相等,而不是像我想的那样比较它们的真实性.

4.1.真值测试

可以测试任何对象的真值,以便在if或while条件中使用或用作以下布尔运算的操作数.

默认情况下,除非对象的类定义了对象,否则将其视为true __bool__()方法返回False或__len__()方法 当与对象一起调用时返回零.

在这里,大多数内置对象被认为是错误的:

  • 常量定义为false:NoneFalse
  • 任何数字类型的零:00.00jDecimal(0)Fraction(0, 1)
  • 空序列和集合:''()[]{}set()range(0)

始终具有布尔结果的操作和内置函数 除非另有说明,否则返回0或False表示false,返回1或True表示true. 说. (重要的例外:布尔运算orand 始终返回其操作数之一.)

4.3.比较

不同类型的对象(不同的数字类型除外)从不 比较相等.

...

一个类的非相同实例通常比较为不相等 除非该类定义了__eq__()方法.

I understand that Python built-in types have a "truthiness" value, and the empty string is considered False, while any non-empty string is considered True.

This makes sense

I can check this using the built-in function bool.

>>> bool("")
False

>>> bool("dog")
True

I can also make use of these truthiness values when using conditionals. For example:

>>> if "dog":
...     print("yes")
...
yes

This is confusing

This doesn't work with the == operator though:

>>> "dog" == True
False

>>> "dog" == False
False

Can anyone explain why == seems to act differently than a conditional?

解决方案

See the truth value testing and comparisons sections of the documentation, excerpted below.

In a nutshell, most things are truthy by default, which is why bool("dog") is true. The == operator compares two objects for equality, as opposed to comparing their truthinesses, as I assume you had expected.

4.1. Truth Value Testing

Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below.

By default, an object is considered true unless its class defines either a __bool__() method that returns False or a __len__() method that returns zero, when called with the object.

Here are most of the built-in objects considered false:

  • 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)

Operations and built-in functions that have a Boolean result always return 0 or False for false and 1 or True for true, unless otherwise stated. (Important exception: the Boolean operations or and and always return one of their operands.)

4.3. Comparisons

Objects of different types, except different numeric types, never compare equal.

...

Non-identical instances of a class normally compare as non-equal unless the class defines the __eq__() method.

这篇关于了解python字符串的真实性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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