Occasionally people post complaining about the lack of a
"repeat...until" structure in python.  I thought about it and came up
with this recipe that I like.  The only ugly thing is having to use
lambdas, otherwise it's very terse and readable.  Tell me what you
think, and if anyone besides me thinks they might use it, I'll post it
to the python cookbook.

Thanks for your time,
Tom

class Until:
    """
    >>> i = 0
    >>> while Until(lambda: i<3):
    ...     print "hello"
    ...     i += 1
    hello
    hello
    hello
    >>> while Until(lambda: i<2):
    ...     print "hello"
    hello
    """
    yet = True
    def __init__(self, mybool):
        if self.__class__.yet or mybool():
            self.__class__.yet = False
            self.ans = True
        else:
            self.__class__.yet = True
            self.ans = False

    def __nonzero__(self):
            return self.ans

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to