布尔在Python [英] Boolean in Python

查看:156
本文介绍了布尔在Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python中是否实际包含一个布尔值?我知道,你可以这样做:

 检查= 1
如果检查:
    #做东西

不过,我很迂腐,享受在Java中看到的布尔值。例如:

 布尔检查;
如果(someDecision)
{
    检查= TRUE;
}
如果(检查)
{
    //一些东西
}

有没有这样的事情,因为这在Python?我似乎无法找到文档中这样的事情。


解决方案

 检查=无#没有必要如果some_decision:
    检查=真如果检查:
    #一些东西

有关详细信息:<一href=\"http://docs.python.org/library/functions.html#bool\">http://docs.python.org/library/functions.html#bool

您code工作过,因为1转换为真时,必要的。
其实Python中没有一个boolean型的很长一段时间(如在老C),以及一些程序员利用窗台的整数,而不是布尔值。

Does Python actually contain a Boolean value? I know that you can do:

checker = 1
if checker:
    #dostuff

But I'm quite pedantic and enjoy seeing booleans in Java. For instance:

Boolean checker;
if (someDecision)
{
    checker = true;
}
if(checker)
{
    //some stuff
}

Is there such a thing as this in Python? I can't seem to find anything like it in the documentation.

解决方案

checker = None # not necessary

if some_decision:
    checker = True

if checker:
    # some stuff

[Edit]

For more information: http://docs.python.org/library/functions.html#bool

Your code works too, since 1 is converted to True when necessary. Actually Python didn't have a boolean type for a long time (as in old C), and some programmers sill use integers instead of booleans.

这篇关于布尔在Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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