silly idea - interesting problem

Andrew Dalke dalke at dalkescientific.com
Sun Oct 21 03:54:42 EDT 2001


scott:
>P.S.: Remember when Python used to differentiate itself from line noise
>(sorry, I meant Perl) with a philosophy that went something like
>"There's only one right way to do it."?

Quoting from the Python Way, as channeled by its guru the uncle Tim,

13. There should be one -- and preferably only one -- obvious way
     to do it.

Yet as we read these words, let us also reflect on verse 5.

5. Flat is better than nested.

Here we see the two sayings seemingly opposed.  For we start
with the traditional

firstWord = 'test'
secondWord = 'dust'
endString = ''
i = 0
while i < len(firstWord):
    endString = endString + firstWord[i] + secondWord[i]
    i = i+1
print endString

a chunk of code that could have remained unchanged for lo these
past 10 years while Python has encoiled more of the faithful.  But
behold the loss of flatness - a branching that can cause the
errors of Murphy to hold sway.

This form prevails so the practictioners of structured programming
did promote refactorization into functions.  Struggling mightily
they failed to come up with readable function names, for the results
were as long as the code it replaced

  def print_word_pairs(firstWord, secondWord):
     ...

  print_word_pairs(firstWord, secondWord)

and were rarely reused enough to warrant being a function.  Those
that tried to generalize further the code fragments did so at
the pain of straying from the third way.

3. Simple is better than complex.

With the second coming of Python we have received a newer, deeper
form of expression.  One which allows us to be flat and simple, and
which has become the newly revealed obvious way.

For this allows us to say

  letter_pairs = [x+y for (x,y) in zip('test', 'dust')]
  print ''.join(letter_pairs)

And thus the seeming contradiction between the 5th and 13th
ways is no more.

But bear these words to heart - do not use more than three or
four clauses in a list comprehension or on a line lest you face
barbs of the gaurdians of the code review.

This lesson in the revealed teachings of the Python Way has
been brought to you by

                    Andrew
                    dalke at dalkescientific.com
  :)






More information about the Python-list mailing list