A little disappointed so far

Tom Bryan tbryan at python.net
Tue May 20 08:02:45 EDT 2003


Graham Nicholls wrote:

>>> > What things seem very hard?
>>> Regular expressions.
>> 
>> In most cases where you'd need them in other situations, you can avoid
>> them. I avoid regexps at all costs.
> No, I completely disagree. Without regexps, I may as well use c, which I
> can code in my sleep.

I'm not sure that's what he meant.  The string methods are quite powerful 
and clear.  (See the string module, but know that most of the string 
module's functions are now methods on the strings themselves.)  The os.path 
module is also quite handy.  Finally, since strings are immutable lists of 
characters, you can index and slice them. 

What I found in porting many Perl scripts to Python is that I could (and 
should!) replace about half of the regular expressions with something else 
from Python.  I think that since regexps are built into Perl, many Perl 
programmers use regexps to do something as simple as 

# does the string end in a slash
if ($myString =~ /\/$/) 

Where in Python, that would be
if myString[-1] == '/':

For problems that really require a regular expression, use the re module.  
It is as powerful as Perl regexps, but it will certainly not be quite so 
convenient.  That was my experience anyway.  

Having switched from Perl to Python for most of my shell and string 
processing needs, I can say two things.

1) For occasional use, Python is much easier to remember.  I use it every 
few weeks.  When I used to do that with Perl, I kept forgetting important 
built in stuff and special cases.  

2) For steady use, Python nudged me toward better and more reuse.  A script 
file is already in a module with its own namespace, ready to be reused.  To 
reuse a script, I started to package work into functions.  I built data 
structures (classes), often attaching behavior (methods) to them over time.  
After a few weeks, I found that writing code in Python had become much 
faster because I already had packaged up a bunch of code in a reusable 
manner.  With access to those modules, I was usually able to concentrate on 
the interesting bits of a problem because all of the "easy" stuff was 
already done in the modules.  

It sounds like you do a lot of Perl, so the first point probably doesn't 
really matter to you.

As far as the second point, if you already have a bunch of packages or 
modules in Perl, then you're already there in Perl.  I just found this type 
of packaging for reuse to be difficult in Perl.  In Python, it's just the 
opposite.  I find it difficult to write code without accidentally packaging 
it well.  That's why everything I write in Python starts out like this

def main():
        pass

if __name__ == '__main__':
        main()

That way, I can start writing code for main.  If I find something that seems 
generally useful, I push it out into a function or a class.  Then, with 
that "if __name__ == '__main__'" guard, other modules can safely import 
this module to access that function or class.  After doing this for a 
while, I move similar functions and classes into their own module.  Since 
main already just calls a function, say foo(), I just move the definition 
of foo to some common module Bar and write
from Bar import foo
I find that Bar grows quickly into something useful for even small projects.  
Try it for a few weeks and see whether you end up growing your own in house 
modules full of stuff that you can reuse.

---Tom





More information about the Python-list mailing list