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

Sean 'Shaleh' Perry shalehperry at attbi.com
Wed Feb 20 11:06:09 EST 2002


On 20-Feb-2002 Russell Nelson wrote:
> There are two Python ideoms that bother me:
> 
>     while 1:
>         line = file.readline()
>         if not line: break
> 
> and:
> 
>     matched = re.match(r'egexp(.*)', line)
>     if matched:
>         print matched.group(1)
> 
> I'd really, really like it if you could do one(1) assignment within
> the boolean expression in a while and an if.  Like this:
> 

this is a design decision in the language.  By not allowing this style of
programming we do not have the ever so fun:

if foo = 2:
   handle_foo()

type bugs.  Sure there are ways around it.  But python's strength is that it is
easy to write good code and easy on the reader.  Hiding assignments in loops
and tests do not make it easier for the next reader.

With 2.x and later the first problem has largely gone away.  The new idiom is
more like:

for line in file.xreadlines():
    parse(line)

xreadlines() does not load the whole file into memory and is generally about as
efficient as you can get.

The second item does however make re's more annoying in python than they are in
other languages where re's are native to the language.




More information about the Python-list mailing list