conditional expression sought

Elaine Jackson elainejackson7355 at home.com
Thu Jan 29 20:06:55 EST 2004


Sorry to take so long but I wasn't sure what a "unit test" was (the other guy's
post clarified it). Tell me if this isn't what you're looking for:

falsies=[0,0.0,[],(),{},'',None]
truies=[49,3.14,[1,2,3],(4,5,6),{7:8,9:10},'nonempty']

def demo(A,B):
    print "If A is ",A
    print "and B is ",B
    print "then (A[0] and B[0]) or (A[1] and B[1]) or (A[2] and B[2]) = ",
    print (A[0] and B[0]) or (A[1] and B[1]) or (A[2] and B[2])

A=[]
from random import randint
for i in range(3):
    A.append(bool(randint(0,1)))
B=truies[0:3]
demo(A,B)

A=[False,False,False]
B=falsies[0:3]
demo(A,B)
print "I would have liked this to be B[2] = ",B[2]


"Mark McEahern" <mark at mceahern.com> wrote in message
news:mailman.1000.1075402284.12720.python-list at python.org...
| Elaine Jackson wrote:
|
| >If bool(B_i)==True for 1<=i<=n and j is the smallest i with bool(A_j)==True,
| >then the evaluation of (A_1 and B_1) or ... or (A_n and B_n) returns B_j
without
| >evaluating any other B_i. This is such a useful mode of expression that I
would
| >like to be able to use something similar even when there is an i with
| >bool(B_i)==False. The only thing I can think of by myself is ( (A_1 and
[B_1])
| >or ... or (A_n and [B_n]) )[0], and I can't be satisfied with that for
obvious
| >reasons. Does anybody know a good way to express this? Any help will be mucho
| >appreciado.
| >
| Why not write a unit test that demonstrates the behavior you want?
| It'll then likely be obvious to someone both what your problem is and
| what a likely solution is.
|
| Cheers,
|
| // m
|





More information about the Python-list mailing list