tags:

views:

264

answers:

5

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.

+7  A: 
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.

Bastien Léonard
It is usually simpler to use checker=(some_decision) (e.g. checker=(a<b)), instead of an 'if'.
MAK
+12  A: 

The boolean builtins are capitalized: True and False.

Note also that you can do checker = bool(some_decision) as a bit of shorthand -- bool will only ever return True or False.

It's good to know for future reference that classes defining __nonzero__ or __len__ will be True or False depending on the result of those functions, but virtually every other object's boolean result will be True (except for the None object, empty sequences, and numeric zeros).

Mark Rushakoff
Thank you very much for the insight. As the popular vote has swung to the other answer, I awarded Bastien the 'Accepted Answer'. However yours was most helpful! :)
day_trader
bravado: what's the point not accepting the answer you find most helpful? It's totally irrelevant if another answer is upvoted more.
ΤΖΩΤΖΙΟΥ
+1  A: 

Boolean types are defined in documentation http://docs.python.org/library/stdtypes.html#boolean-values

Quoted From doc:

"""Boolean values are the two constant objects False and True. They are used to represent truth values (although other values can also be considered false or true). In numeric contexts (for example when used as the argument to an arithmetic operator), they behave like the integers 0 and 1, respectively. The built-in function bool() can be used to cast any value to a Boolean, if the value can be interpreted as a truth value (see section Truth Value Testing above).

They are written as False and True, respectively."""

So in java code remove braces, change true to True and you will be ok :)

Anurag Uniyal
+1, that's the kind of quote I was looking for for my own answer.
Bastien Léonard
+1  A: 

True ... and False obviously.

Otherwise, None evaluates to False, as does the integer 0 and also the float 0.0 (although I wouldn't use floats like that). Also, empty lists [], empty tuplets (), and empty strings '' or "" evaluate to False.

Try it yourself with the function bool():

bool([])
bool(['a value'])
bool('')
bool('A string')
bool(True)  # ;-)
bool(False)
bool(0)
bool(None)
bool(0.0)
bool(1)

etc..

Terje Dahl
A: 

Yes, there is a bool data type (which inherits from int and has only two values: True and False).

But also Python has the boolean-able concept for every object, which is used when function bool([x]) is called.

See more: object.nonzero and boolean-value-of-objects-in-python.

van