Why does list have no 'get' method?

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Fri Feb 8 19:27:48 EST 2008


Carl Banks:
> I think booleans should be completely disjoint from all other types,
> as they are in Java.  "and",  "or", and "not" should accept only boolean
> operands and return only boolean results.

I'd like "or" and "and" (and "not") to return booleans only, to avoid
bugs and make their meaning more transparent, but I like the fact that
empty containers like "" [] (,) {}  are seen as false:

x = []
if x:
    ...
else:
    ... // goes here

bool([]) ==> False

So I don't mind "and", "or" to take non-booleans too as input:

[] or "aa" ==> True

I think that's a good compromise between practicality, and avoiding
bugs.

"not" already works that way, it takes other objects too as input, but
its output is always a bool:

>>> not "aa"
False
>>> not ""
True

Bye,
bearophile



More information about the Python-list mailing list