Feedback on Until recipe

Paul McGuire ptmcg at austin.rr.com
Wed Apr 25 22:11:37 EDT 2007


I moved the state into the Until instance vs. the Until class, so that
it now supports nesting.  But the calling syntax is even uglier - but
necessary to avoid creating a new Until instance each time around.
That is "while Until(blah):" evaluates and constructs a new Until
instance each time around the loop, so that is why the state had to be
kept in a class-level variable in your original design (which is why
nesting doesn't work).

class Until:
    def __init__(self, mybool):
        self.lastTest = True
        self.mybool = mybool

    def __nonzero__(self):
        ret,self.lastTest = self.lastTest,self.mybool()
        return ret

i = 0
u1 = Until(lambda : i < 0 )
while u1:
    print "hello"
    i += 1
    j = 0
    u2 = Until(lambda : j < 0 )
    while u2:
        print "byebye"
        j += 1




More information about the Python-list mailing list