syntax philosophy

Dave Brueck dave at pythonapocrypha.com
Mon Nov 17 23:06:52 CET 2003


Tuang wrote:
> I'm checking out Python as a candidate for replacing Perl as my "Swiss
> Army knife" tool. The longer I can remember the syntax for performing
> a task, the more likely I am to use it on the spot if the need arises.
> If I have to go off and look it up, as I increasingly have to do with
> Perl's ever hairier syntax, I'm more likely to just skip it, making me
> even less likely to remember the syntax the next time.
>
> So I hear that Python is easier to remember between uses than Perl. So
> far, I like what I see. Iterators and generators, for example, are
> great. Basic loops and other things are very convenient in Python.
>
> But I'm surprised at what you apparently have to go through to do
> something as common as counting the frequency of elements in a
> collection. For example, counting word frequency in a file in Perl
> means looping over all the words with the following line:
>
> $histogram{$word}++;

Hi Tuang,

Here's my _opinion_: Perl is especially geared towards text processing, where
maybe counting word frequency is fairly common. Python is more of a general
purpose programming language, in which counting the word frequency is a pretty
rare operation (I can't remember needing to do that more than once or twice in
the past several years). As such, it probably doesn't make sense to support
that feature at the language level - it would burden a lot of people with
knowing syntax they'd rarely use.

[snip]
> But I guess I'm making assumptions about what Python's philosophy
> really is. I would expect that a language with something as nice as
>
> [x**3 for x in my_list]

Building a list out of another list, however, is far more common, hence (in my
view at least) the appropriateness of syntax-level support.

> Is this just something that hasn't been done yet but is on the way, or
> is it a violation of Python's philosphy in some way?

Python can automatically import custom modules and functions on startup (search
for information on the site module), so if I were you I'd write a
WordHistorgram function in my custom site module just once and never look back.
The added benefit is that

histogram = WordHistogram(text)

is much more readable to me as well as others than

$histogram{$word}++;

> Since I'm trying to choose a good Swiss-Army-knife programming
> language, I'm wondering if this Python histogram technique is the sort
> of thing that bothers Pythonistas and gets cleaned up in subsequent
> versions because it violates Python's "philosophy" or whether, on the
> contrary, it's just the way Pythonistas like to do things and is a
> fair representation of what the community (or Guido) wants the
> language to be.

My impression is that features generally get added if (1) there is a good
enough case for their broad usefulness and (2) they don't overly compromise the
relatively clean syntax of the language. In this specific example, the
histogram-builder function fails both tests, so such functionality would best
live in some separate module. If over time enough people wanted it, it could
always be shipped as one of the "standard" Python modules.

-Dave






More information about the Python-list mailing list