Bragging about Python

Szabolcs Nagy nszabolcs at gmail.com
Thu Jun 7 18:32:22 EDT 2007


Cameron Laird wrote:
> In article <46681369$0$12126$3b214f66 at tunews.univie.ac.at>,
> Mathias Panzenboeck  <e0427417 at student.tuwien.ac.at> wrote:
>    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 ...

actually i don't like when a tutorial uses over complicated cute names
if the context is obvious (fibonacci) then we don't need to add
'parent_rabbits' and such identifiers
eg i find more readable and clear the following:

def fib():
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a + b

for (n, fn) in enumerate(fib()):
    if n > 100:
        break
    print "F[%d] = %d" % (n, fn)

ymmv




More information about the Python-list mailing list