two ideoms at one blow: line-reading and regexp-matching

Christian Tanzer tanzer at swing.co.at
Thu Feb 21 01:49:16 EST 2002


Russell Nelson <nelson at crynwr.com> wrote:

> There are two Python ideoms that bother me:
>
>     while 1:
>         line = file.readline()
>         if not line: break

No need for this one in 2.2.

> and:
>
>     matched = re.match(r'egexp(.*)', line)
>     if matched:
>         print matched.group(1)

It's trivial to write a class Regexp which allows this style of code:

    pat = Regexp(r'egexp(.*)')
    ...
    if pat.match (line) :
        print pat.last_match.group (1)

> Should I write up a PEP or has this already been discussed and
> dismissed?

It's been discussed time and again. Writing a PEP and having it
dismissed might make future discussions a bit shorter <wink>. And
then, it might not.

-- 
Christian Tanzer                                         tanzer at swing.co.at
Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92






More information about the Python-list mailing list