Dijkstra on Python

Paul Rubin phr-n2002b at NOSPAMnightsong.com
Fri Aug 16 18:04:53 CEST 2002

"RPM1" <rpm1deletethis at nospamfrontiernet.net> writes:
> > The real meaning of "There should be one obvious way to do it" is that
> > the Python community values:
> >  * Sticking to common idioms
> >  * Avoiding the use of clever constructions
> >  * Avoiding coding in a way that requires a reader to "decode" it, if
> >    it can be avoided
> Hmmm.  The above three statements don't seem to have
> a strong correlation to "one obvious way to do it", IMHO.
> It seems more like you're talking about readablility.
> Readablility *is* unquestionably a Python strong point that
> Perl is lacking.  Readablility may come from limited syntax
> choices but I don't think that implies limited algorithmic choices.
> I propose Python's motto should be:
> If you can read this:
>     it must be Python

Fair enough.  The readable and one-and-only-obvious-way to add up the
first n values of an integer-returning function is:

   sum = 0   
   for i in range(n):
      sum += f(i)

If the function returns a string instead (remember that adding strings
means concatenation), the one-and-only-obvious-way to do it changes
only slightly:

   sum = ''
   for i in range(n):
     sum += f(i)

Whoops!  The second example works, but has quadratic running time!
So real Python programs end up full of hair like

   sum = ''.join([f(i) for i in range(n)])

which seems to me to require "decoding".

I conclude from this that "one obvious way to do it" implies that Python
needs a mutable (or at least extendable) string type, that supports +=
the way list objects support appending.  Maybe the above example could be:

   sum = xstr('')       # xstr makes an extendable string that supports +=
   for i in range(n):
     sum += f(n)

PEP anyone?  

More information about the Python-list mailing list