Equivalent code to the bool() built-in function
rosuav at gmail.com
Mon Apr 18 02:52:31 CEST 2011
On Mon, Apr 18, 2011 at 10:22 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
>>>> types = [str, complex, float, bool]
>>>> [f(x) for f, x in zip(types, (1, 2, 3, 4))]
> ['1', (2+0j), 3.0, True]
I believe this one would work fine with a function defined as per OP -
zip takes callables, which could be types or functions.
> * if flags are written 0 and 1, that frequently fools people into
> thinking they are meant to be understood as *integer* values rather
> than boolean values, and they will do arithmetic on those flags;
Depending on what you mean by arithmetic, that can be a good thing.
I've frequently, in various languages, used booleans as though they
were integers - for instance, indexing a list with an integer plus a
condition. Contrived example:
mode = 2
print ("default", "default-root",
For debugging output, where I want to keep it as self-contained as
possible, constructs like these are very handy - especially as they
can be embedded inside other expressions.
Every language I've used except BASIC has used True == 1, False == 0,
but I'm sure there are some which use other values. If True == -1,
then you simply subtract the bool from the int instead of adding them.
If it's something else, you arrange the array accordingly.
> * there was never any consistency, people would write any of:
> true = 1; false = 0
> FALSE = 0; TRUE = not FALSE
> True = -1; False = not True
Just as long as someone doesn't use:
true = 1; false = -1
which results in the annoying situation that:
if x == false:
is different from:
But on the plus side, TheDailyWTF.com is never short of publishable material...
More information about the Python-list