syntax philosophy

Tuang tuanglen at
Mon Nov 17 22:29:16 CET 2003

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:


The creation of the dictionary, the creation of new elements, the
initialization to zero, are all taken care of automatically. You just
tell it to start counting and the rest is taken care of for you. And
the incrementing is just a simple "++".

In Python, apparently you have to first remember to declare your
dictionary outside the loop:

histogram {}

Then within the loop you use the following construct:

histogram[word] = histogram.get(word, 0) + 1

That's quite a bit hairier and it requires remembering to use braces
{}, then square brackets [], then parentheses (), and accessing the
dictionary via two different techniques in the same line.

This seems sort of unPythonesque to me, given the relative cleanliness
and obviousness (after seeing it once) of other common Python

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]

would want to use something like:


or even combine them into something like:

{histogram[word]++ for word in my_list}

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?

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.


More information about the Python-list mailing list