Equivalent code to the bool() built-in function

Daniel Kluev dan.kluev at gmail.com
Mon Apr 18 05:16:38 CEST 2011


On Mon, Apr 18, 2011 at 12:46 PM, Dave Angel <davea at ieee.org> wrote:
> He didn't say that the function will call the bool() type (constructor), but
> that it will use the bool type;

Actually, he did say exactly that
> Any boolean expression is going to be _calling the built-in ‘bool’ type constructor_
(underscores are mine)

>The one that may not is the function bool().
Its not function, its type. There is no wrapper, bool(x) is direct
constructor call.

> Once bool has been reassigned, calling it may not return True or False any more.
Not sure what did you want to show with this example. You just
assigned name in locals() namespace. Boolean type itself didn't change
because of that and would still call PyObject_IsTrue() and return
according constant. Sure, python allows to change namespaces in very
flexible way, but we are talking about specific objects (PyBool_Type)
rather than pointers to them.

> in other words, it will return True or False.
Well, his code explicitly returns True or False, so this was not doubted.

Although I agree with Ben that this doesn't have any practical
meaning. bool() is more readable and implementation-independent way to
do explicit casting to boolean than the hack in OP.

-- 
With best regards,
Daniel Kluev



More information about the Python-list mailing list