feedback on Until recipe

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas Nelson

    #1

    feedback on Until recipe

    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__(sel f):
    return self.ans

Working...