syntax philosophy

Diez B. Roggisch deets_noospaam at web.de
Mon Nov 17 23:06:03 CET 2003


Hi,

> 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}++;

Is this all thats needed - or is it actually something like this:

for $word in $words:
   $histogram{$word}++

(I've never used perl, so I blatantly mix python/perl syntax here - but I
think you'll understand anyway :))

> 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.

>From what I understand in your example, the power of perl for this example
comes from its untypedness - having an unbound variable histrogram and
accessing it with the {}-operator seems to implicitly make it an
dictionary. The same seems true for accessing an nonexistant key - it
doesn't provoke an exception, instead it returns nothing (is there a
keyword/constant for that?) - and obviously the plus-operator implicitly
casts that to a 0, so it can perform the addition.

You won't see this in python - as python is a strong-typed
programming-language. Thats not to be confused beeing a static typed
language - which it is not.

So, to answer your question - I think thats the way we like it :) After all,
you can always put your code into a histogram-function and call that -
which I don't find too much work ;-) 

Regards,

Diez




More information about the Python-list mailing list