Why bool( object )?

Arnaud Delobelle arnodel at googlemail.com
Sat May 2 04:23:58 EDT 2009


Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:

> On Fri, 01 May 2009 15:03:30 -0700, Aaron Brady wrote:
>
>> On May 1, 4:30 am, Steven D'Aprano <st... at REMOVE-THIS-
>> cybersource.com.au> wrote:
>>> On Fri, 01 May 2009 16:30:19 +1200, Lawrence D'Oliveiro wrote:
>>> > I have never written anything so unbelievable in my life. And I hope
>>> > I never will.
>>>
>>> I didn't say you did. If anyone thought I was quoting Lawrence's code,
>>> I'd be surprised. It was not my intention to put words into your mouth.
>>>
>>> But seeing as you have replied, perhaps you could tell us something.
>>> Given so much you despise using non-bools in truth contexts, how would
>>> you re-write my example to avoid "a or b or c"?
>>>
>>> for x in a or b or c:
>>>     do_something_with(x)
>
> [...]
>> I don't think it would be very common to write Steven's construction for
>> arbitrary values of 'a', 'b', and 'c'.
>
> I don't care about "arbitrary values" for a, b and c. I don't expect a 
> solution that works for (say) a=None, b=5, c=[]. I'm happy to restrict 
> the arguments to all be arbitrary sequence-like objects.
>
> I'm even happy for somebody to give a solution with further restrictions, 
> like "if I know before hand that all three are lists, then I do blah...". 
> But state your restrictions up front.

If a, b, c are names or literals, then I guess you could do this:

for seq in a, b, c:
    if seq: break
for x in seq:
    do_something_with(x)

I have never been in a situation where I needed something like this,
though.
-- 
Arnaud



More information about the Python-list mailing list