RE strings (was: Variable Interpolation - status of PEP 215)

Peter Hansen peter at engcorp.com
Thu Jun 20 21:04:09 EDT 2002


Cimarron Taylor wrote:
> 
> I like it!
> 
> To me
> 
>   m = re'^EMP:([^,]*),([^,]*),([^,]*),([^,]*)$'.match(line)
>   if m:
>         ...
> 
> is much clearer and more maintainable than
> 
>   import re
>   emp_re = re.compile(r'^EMP:([^,]*),([^,]*),([^,]*),([^,]*)$')
>   m = re.match(emp_re, line)
>   if m:
>       ...

But is it really much clearer and more maintainable than

import re
m = re.match(r'^EMP:([^,]*),([^,]*),([^,]*),([^,]*)$', line)
if m:
    ...

Given that the only real difference is you skip an import, and
at the cost of modifying the language definition in an unusual
manner, is there any point to this at all?

-Peter



More information about the Python-list mailing list