Equivalent code to the bool() built-in function
Duncan Booth
duncan.booth at invalid.invalid
Mon Apr 18 06:01:22 EDT 2011
Steven D'Aprano <steve+comp.lang.python at pearwood.info> wrote:
> So in Python 2.2, Python introduced two new built-in names, True and
> False, with values 1 and 0 respectively:
>
> [steve at sylar ~]$ python2.2
> Python 2.2.3 (#1, Aug 12 2010, 01:08:27)
> [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> True, False
> (1, 0)
>>>> type(True)
><type 'int'>
>
Also in Python 2.2 (and I think earlier but I don't have anything
earlier to check) there was an interesting property that while all other
integers from -1 to 99 were optimised to share a single instance, there
were actually 2 instances of 0 and 1:
Python 2.2.3 (#1, Sep 26 2006, 18:12:26)
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-56)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 2-1 is 1
1
>>> True is 1
0
>>> (True is 1) is 0
0
>>> (True is 1) is False
1
The code for the win32api made use of this fact to determine whether to
pass int or bool types through to COM methods.
--
Duncan Booth http://kupuguy.blogspot.com
More information about the Python-list
mailing list