Equivalent code to the bool() built-in function

Ben Finney ben+python at benfinney.id.au
Sat Apr 16 17:38:33 EDT 2011


Chris Rebert <clp2 at rebertia.com> writes:

> That is, `True if x else False` conceptually gets compiled down to
> `True if bool(x) == 1 else False` (but without doing a run-time lookup
> of "bool").

It won't look up the *name* ‘bool’, but it will use that object. Any
boolean expression is going to be calling the built-in ‘bool’ type
constructor.

So the answer to the OP's question is no: the function isn't equivalent
to the type, because the OP's ‘bool_equivalent’ function necessarily
uses the built-in ‘bool’ type, while the reverse is not true.

-- 
 \        “Perchance you who pronounce my sentence are in greater fear |
  `\   than I who receive it.” —Giordano Bruno, burned at the stake by |
_o__)  the Catholic church for the heresy of heliocentrism, 1600-02-16 |
Ben Finney



More information about the Python-list mailing list