A little disappointed so far

Skip Montanaro skip at pobox.com
Sun May 18 21:46:53 EDT 2003


    >> What things seem very hard?  

    Graham> Regular expressions.

Regular expressions are generally not easy beasts to get along with.  Python
doesn't build them into the language like Perl, but they work just fine and
in a straightforward way, e.g.:

    >>> import re, string
    >>> match = re.search("(a+)", string.digits)
    >>> print match
    None
    >>> match = re.search("(a+)", string.letters)
    >>> print match
    <_sre.SRE_Match object at 0x826ea78>
    >>> print match.group(1)
    a

In contrast, every time I want to use a regular expression in Perl, I wind
up executing

    man perlop

because Perl's blizzard of operators just often don't make sense to me.  To
each his own though.

    Graham> Running external programs.

os.system()
os.popen()

Again, the module index is your friend.

    Graham> Opening sockets.

Most of the time I find I want to use a higher level protocol, but dealing
with raw sockets isn't that hard either.  Take a look at the examples in the
Demo/sockets directory in the distribution.  Python has modules available to
support a broad range of higher level TCP protocols (ftp, http, xmlrpc, etc).

    Graham> No case statement - or is it simply that my documentation is out
    Graham> of date? I _know_ I can do if elif else constructs, but in a
    Graham> language which prides itself in its readability, this is
    Graham> laughable.

You don't really think it hasn't been considered do you?  It turns out to be
difficult to incorporate into the language in a syntactically clean way.
Guido (bless his vacationing soul) has been pretty relentless in is
resistance to just tossing stuff into Python.  Many, many electrons have
been burned on the topic of switch/case statements alone.  For a reasonable
summary, check out PEP (Python Enhancement Proposal) 274:

    http://www.python.org/peps/pep-0274.html

or

    http://www.python.org/peps/

to peruse the entire PEP index.

    Graham> try and except you'll say - but these _seem_ to be very generic,
    Graham> whereas I want to deal with a very specific error.  Now, I may
    Graham> be being unfair,

You seem to be falling into a fairly common trap.  Many people come to
Python from other languages and want Python to work the way those other
languages work.  Python was developed with some definite tenets in mind (try
executing "import this" at the interpreter prompt sometime).  Once you
understand those tenets and some of the core constructs in the language
(like exceptions), things will probably fall into place better.

    Graham> BTW,
    Graham> open ($file) || die "Couldn't open $file" 
    Graham> strikes me as rather readable. And concise.

So then you should be using Perl.  Of course, it's kind of hard to recover
in a die statement. ;-)  With

    try:
        f = open(somefile)
    except IOError, msg
        do_something_with(somefile, msg)

you have the chance to try another tactic to satisfy your user's request:

    >>> try:
    ...   f = file("/etc/passwd2")
    ... except IOError, msg:
    ...   print msg
    ...   ans = raw_input("continue? ")
    ...   if ans[0:1].lower() == 'n':
    ...     raise
    ... 
    [Errno 2] No such file or directory: '/etc/passwd2'
    continue? n
    Traceback (most recent call last):
      File "<stdin>", line 2, in ?
    IOError: [Errno 2] No such file or directory: '/etc/passwd2'

Clearly, you can recover from errors in Perl as well, but I think Python
allows you to handle exceptional conditions better than any other language
I've encountered.

    Graham> I really don't wan't to attack python, but your attitude strikes
    Graham> me as somewhat less than helpful, and I posted for some help,
    Graham> perhaps in looking further to see that I was mistaken, rather
    Graham> than for a flame war.

I think everyone who's replied so far has been quite accommodating,
especially given that your initial message was a bit vague about what you
didn't like.

    >> Chances are if you're doing something that you think is incredibly
    >> difficult, there's either 1. something preexisting that does it for
    >> you or 2. you're approaching the problem the wrong way.

    Graham> With 25 years programming experience, I have a good idea by now
    Graham> about my approach to programming, but thanks for the patronising
    Graham> comment, anyway.

Trust me, you're not the only person around here with 25 (or more) years of
programming experience. ;-) And it wasn't a patronizing comment.  You'll get
more mileage out of Python once you learn the Pythonic way of doing things.
Be a willow, not an oak.  Bend a bit with the wind.  Don't buck it.

Say, are you an infiltrator from the PSU, trying to boost the posting
frequency of comp.la

Skip





More information about the Python-list mailing list