help converting some perl code to python

jgardner at jonathangardner.net jgardner at jonathangardner.net
Fri Nov 4 10:33:21 EST 2005


The '..' operator is the flip-flop operator in perl. (It is rarely
used.) It is exactly the same as the 'range' type operator. It returns
false until the first condition is met, then it returns true until the
last condition met, then it returns false.

You could create a flip-flop with a python closure (t_cond and f_cond
are functions that take a value and return True of False)

def make_flip_flop(t_cond, f_cond):
    state = [False]
    def flip_flop(val):
        if state[0] and f_cond(val):
            state[0] = False
        elif not state[0] and t_cond(val):
            state[0] = True
        return state[0]
    return flip_flop




More information about the Python-list mailing list