simultaneous assignment

John Salerno johnjsal at NOSPAMgmail.com
Tue May 2 14:52:48 EDT 2006


Steve R. Hastings wrote:

> Anyway, the major point I want you to take away from all this: it doesn't
> matter whether the "is" test succeeds or fails, if all you care about is
> the *value* of a variable.  Python reuses object references to save
> memory, because this doesn't affect expressions that only care about the
> *value*.  Your logic tests only care about True vs. False; they don't care
> about the underlying implementation of how True and False are expressed.

Great explanation. Thanks!

> This is a simple problem.  I suggest you loop over the list and simply
> count how many values are false, and how many are true.  I just wrote and
> tested this function in about a minute.

Yeah, after trying some crazy things, I just wrote it this way:

def truth_test(seq):
     truth = 0
     for item in seq:
         if item:
             truth += 1
     if truth == 1:
         return True
     else:
         return False

Not sure I like having to keep a counter though, but the other stuff I 
did was really convoluted, like checking to see if the first item was 
True, and if it was, popping it from the list and iterating over the 
rest of the items (needless to say, the in-place change wasn't helpful).



More information about the Python-list mailing list