Bragging about Python

Cameron Laird claird at lairds.us
Thu Jun 7 14:19:06 EDT 2007


In article <46681369$0$12126$3b214f66 at tunews.univie.ac.at>,
Mathias Panzenboeck  <e0427417 at student.tuwien.ac.at> wrote:
>Steve Howell schrieb:
>> --- "pinkfloydhomer at gmail.com"
>> <pinkfloydhomer at gmail.com> wrote:
>> 
>>> Is there a resource somewhere on the net that can be
>>> used to quickly
>>> and effectively show Python's strengths to
>>> non-Python programmers?
>>> Small examples that will make them go "Wow, that
>>> _is_ neat"?
>>>
>> 
>> 15 small programs here:
>> 
>> http://wiki.python.org/moin/SimplePrograms
>> 
>
>IMHO a few python goodies are missing there.
>e.g. generator functions:
>
># generic fibonacci without upper bound:
>def fib():
>    parent_rabbits, baby_rabbits = 1, 1
>    while True:
>        yield baby_rabbits
>        parent_rabbits, baby_rabbits = baby_rabbits, parent_rabbits +
>baby_rabbits
>
>
># only calculate and print the first 100 fibonacci numbers:
>from itertools import islice
>
>for baby_rabbits in islice(fib(),100):
>    print 'This generation has %d rabbits' % baby_rabbits

Good point.

This example intrigues me.  When I saw the first couple of lines,
I expected

   def fib():
       generation, parent_rabbits, baby_rabbits = 1, 1, 1
       while True:
           yield generation, baby_rabbits
           generation += 1
           parent_rabbits, baby_rabbits = \
                  baby_rabbits, parent_rabbits + baby_rabbits

    for pair in fib():
        if pair[0] > 100:
            break
        print "Generation %d has %d (baby) rabbits." % pair

as more appealing to non-Pythoneers.  I'm still suspicious about
how they're going to react to itertools.islice().  Now, though,
I've begun to question my own sense of style ...



More information about the Python-list mailing list