Positive

bearophile bearophileHUGS at lycos.com
Sun Oct 5 05:54:22 PDT 2008


Anders F Björklund:
> bearophile wrote:
> > Having two ways to do the same thing is generally very bad.
> 
> Depends on your view of the world, that.
> http://en.wikipedia.org/wiki/TIMTOWTDI

If this is the original motto:
"There is more than one way to do it"

then its opposite is probably:
"There should be one way to do it."

But that's not what the Python Zen says, it says:
"There should be one—and preferably only one—obvious way to do it."

There are two differences:
- "preferably" means it's not a hard rule
- "obvious" means that there can be other ways, but the obvious one is generally one. This detail is quite important.

I have tried both Perl and Python and I like Python better; it allows me to very quickly write correct programs, while I don't like Perl, and I find lot of Perl programs around a mess. So I honestly think it's better for D to adopt that zen rul of Python instead of the one of Perl.

The blurb of D refers to Ruby and Python but not to Perl:

>D is a systems programming language. Its focus is on combining the power and high performance of C and C++ with the programmer productivity of modern languages like Ruby and Python. Special attention is given to the needs of quality assurance, documentation, management, portability and reliability.<

One example. In D this can be the obvious way to create an associative array that maps strings to their extended ASCII value:

void main() {
    int[string] d;
    foreach (i; 0 .. 256)
        d["" ~ cast(char)i] = i;
}

In Python there are tons of ways to do that, but in Python3 the obvious one is:

d = {chr(i): i for i in range(256)}

It's short, readable enough, and it's easy to write it correctly the first time. (in Python chars are strings of len 1).

In Python2.5 that code was different:

d = dict((chr(i), i) for i in xrange(256))

That syntax can be used in Python3 too, but it's not the best way to do it anymore :-)

Bye,
bearophile



More information about the Digitalmars-d mailing list