A question about generators

Martin v. Loewis martin at v.loewis.de
Mon Mar 18 16:52:13 EST 2002


rjk at grauel.com (Richard J. Kuhns) writes:

> while 1:
>     inkey = raw_input('Well? ')
>     # there are also several variants like 'generate_exact_matches'
>     for i in generate_appox_matches(inkey):
>         if this_is_interesting(i):
>             do_work(i)
>         break
> 
> There's an assumption that if we find anything with the comparable
> generate_exact_matches(), they're all interesting.

It seems to me that a plain computation of a list of approx_matches
might be appropriate: you do need all of them in the algorithm, so
that would be a bad thing to do only if the approx_matches exceed your
available memory.

I'd write something like

def generate_appox_matches(inkey):
    result = []
    for line in open(filename):
        if appoximately_matches(line, inkey):
             result.append(line)
    return result

Look Ma, no generators!

Regards,
Martin



More information about the Python-list mailing list