Custom Search

Tuesday, April 12, 2011

Python tricks Boolean type

Python tricks Boolean type

The simplest built-in type in Python is the bool type, which can hold only one of two possible objects: True or False:

>>> b = True
>>> type(b)

>>> id(b)
1055887

Because there are only two possible values, the Boolean type is unique. The Python interpreter provides the only two bool objects needed: True and False. Anytime these objects are needed in a Python program, the variable just references one as appropriate.

>>> b = True
>>> id(b)
1055552
>>> bb = b
>>> id(bb)
1055552
>>> bb = True
>>> id(bb)
1055552

The case for the name of the Boolean object is important because true (and false) is undefined:

No comments:

Post a Comment