它是Python的使用作为的bool整数? [英] Is it Pythonic to use bools as ints?

查看:117
本文介绍了它是Python的使用作为的bool整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

等同于 0 是相当于 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)

注意值是如何布尔,但作为 INT

这是这种使用Python化的,还是应该避免?

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

推荐答案

我就奇怪的声音了(因为所有答案都谴责使用的事实,即假== 0 真== 1 ,因为语言的担保),我主张用这个事实来简化你的code是完全正常的。

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的生命周期的过程中,圭多注意到,太多的模块开始分配,例如虚假= 0;真正= 1 和由此产生的样板和无用的变化(后者,因为真假资本是所有的地方 - 一些使用全大写,有的全部小写,有些帽初始)所以推出了 INT 布尔子类,它的常量。

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新手可用于的限制的语言的能力,但圭多坚持认为我们只是被悲观:没有人会理解的Python得很厉害,例如,以避免完全自然的使用作为列表索引,或求和,或其他类似的清透和有用的成语。

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的不可以试图将其逼成其他语言的模具,其功能和preferred风格有很大的不同。 在Python ,真假像1和0 99.9%,不同的完全的在他们的 STR(...)(从而再版(...))的形式 - 为的每个的其他除字符串化操作,只是觉得自由使用它们而不扭曲。这也适用于索引,算术,位操作,等,等,等。

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.

这篇关于它是Python的使用作为的bool整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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