Need better string methods

Stephen Horne steve at ninereeds.fsnet.co.uk
Mon Mar 8 09:37:17 EST 2004


On 8 Mar 2004 02:20:44 -0800, michael at foord.net (Fuzzyman) wrote:

>Remind me of the Perl assertion that it's a better language because
>you can cram functions into huge, indecipherable single lines..... why
>on earth is that an advantage ?

Hardly relevant when the comparison is Ruby, not Perl.

Compare...

clean = line.chomp.strip('.').squeeze.split(/\s*\|\s*/)

with...

clean = line.chomp
clean = clean.strip ('.')
clean = clean.squeeze
clean = clean.split(/\s*\|\s*/)

The regular expression in equally horrible in either case, but then
Python has regular expressions too. When the difference is purely a
matter of how many lines you pack the code into - what your logic is
trying to claim - I really don't think the readability is much
different in either case. If anything, the multiline version is very
slightly obscured by the redundant assignments and repeated use of
'clean', though in this noddy case it isn't really an issue.

Now to do the nearest equivalent in Python that I can come up with...

import re

clean = line.strip ('\n')
clean = clean.strip ('.')
clean = re.sub (' +', ' ', clean)
clean = re.split (' ?\| ?', clean)


Or, if you prefer...

clean = line.strip ('\n')
clean = clean.strip ('.')
clean = ' '.join (clean.split ())
clean = re.split (' ?\| ?', clean)


I'm sorry, but I still don't think Python is clearer. Spreading over
multiple lines is not a magic fix for readability. I'm not by any
means saying 'abandon Python' or anything, but a true Python advocate
would realise that sometimes you can learn a trick from another
language.

And of course no language is perfect. I don't know Ruby myself, but
the impression I have is that it is less suitable for large projects
than Python. One of the strongest Perl advocacy web sites I've ever
read (I forget who's, sorry) included the statement that the user
would still use Perl for 2 minute hack-it-together jobs.

Python is an exceptionally good language, with a lot of rapid
prototyping convenience and yet still well suited to large
applications that have to be maintained. It is the only language I
know where the quick prototypes tend to become the huge, heavily
maintained applications and I *don't* regret it. I think that's pretty
much what that advocacy site says as well.

That doesn't mean that we need a 'not invented here' mentality,
though.


-- 
Steve Horne

steve at ninereeds dot fsnet dot co dot uk



More information about the Python-list mailing list