将布尔值用作整数是 Pythonic 吗?

2022-01-19 00:00:00 python boolean

问题描述

False 等价于 0 并且 True 等价于 1 所以可以这样做:

False is equivalent to 0 and True is equivalent 1 so it's possible to do something like this:

def bool_to_str(value):
    """value should be a bool"""
    return ['No', 'Yes'][value]

bool_to_str(True)

注意 value 如何是 bool 但用作 int.

Notice how value is bool but is used as an int.

这种使用Pythonic还是应该避免?

Is this this kind of use Pythonic or should it be avoided?


解决方案

我会成为奇怪的声音(因为所有答案都在谴责使用 False == 0True == 1,正如语言所保证的那样),因为我声称使用这个事实来简化你的代码是非常好的.

I'll be the odd voice out (since all answers are decrying the use of the fact that False == 0 and True == 1, as the language guarantees) as I claim that the use of this fact to simplify your code is perfectly fine.

从历史上看,逻辑真/假操作倾向于简单地使用 0 表示假,1 表示真;在 Python 2.2 的生命周期过程中,Guido 注意到太多的模块以诸如 false = 0; 之类的赋值开始.true = 1 这产生了样板和无用的变体(后者是因为 true 和 false 的大写无处不在——一些使用全大写,一些全小写,一些大写首字母),因此引入intbool 子类及其 TrueFalse 常量.

Historically, logical true/false operations tended to simply use 0 for false and 1 for true; in the course of Python 2.2's life-cycle, Guido noticed that too many modules started with assignments such as false = 0; true = 1 and this produced boilerplate and useless variation (the latter because the capitalization of true and false was all over the place -- some used all-caps, some all-lowercase, some cap-initial) and so introduced the bool subclass of int and its True and False constants.

当时有相当多的阻力,因为我们中的许多人担心 Python 新手会使用新类型和常量来限制该语言的能力,但 Guido 坚持认为我们只是被悲观:没有人会如此糟糕地理解 Python,例如,为了避免完美自然地使用 FalseTrue 作为列表索引,或在求和中,或其他类似的非常清晰实用的成语.

There was quite some pushback at the time since many of us feared that the new type and constants would be used by Python newbies to restrict the language's abilities, but Guido was adamant that we were just being pessimistic: nobody would ever understand Python so badly, for example, as to avoid the perfectly natural use of False and True as list indices, or in a summation, or other such perfectly clear and useful idioms.

这个帖子的答案证明我们是对的:正如我们所担心的那样,对这种类型和常量的角色的完全误解出现了,人们正在回避,而且,更糟糕的是!敦促其他人避免使用完全自然的 Python 构造来支持无用的旋转.

The answers to this thread prove we were right: as we feared, a total misunderstanding of the roles of this type and constants has emerged, and people are avoiding, and, worse!, urging others to avoid, perfectly natural Python constructs in favor of useless gyrations.

与这种误解的浪潮作斗争,我敦促大家把 Python 当作 Python 来使用,不要试图将它强加到其他语言的模式中,这些语言的功能和风格偏爱完全不同.在 Python 中,True 和 False 有 99.9% 与 1 和 0 相似,完全不同在它们的 str(...) 中(因此 >repr(...)) 形式 -- 对于除字符串化之外的每个 其他操作,请随意使用它们而不会扭曲.这适用于索引、算术、位操作等.

Fighting against the tide of such misunderstanding, I urge everybody to use Python as Python, not trying to force it into the mold of other languages whose functionality and preferred style are quite different. In Python, True and False are 99.9% like 1 and 0, differing exclusively in their str(...) (and thereby repr(...)) form -- for every other operation except stringification, just feel free to use them without contortions. That goes for indexing, arithmetic, bit operations, etc, etc, etc.

相关文章