Feedback on Until recipe

Thomas Nelson thn at mail.utexas.edu
Tue Apr 24 12:08:23 EDT 2007


Occasionally someone posts to this group complaining about the lack of
"repeat ... until" in python.  I too have occasionally wished for such
a construct, and after some thinking, I came up with the class below.
I'm hoping to get some feedback here, and if people besides me think
they might use it someday, I can put it on the python cookbook.  I'm
pretty happy with it, the only ugly thing is you have to use a
lambda.  Ideally i'd like to just see
while Until(i<3)
but that doesn't work.
Please tell me what you think, and 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):  #note i still equals 3 here
    ...     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




More information about the Python-list mailing list