Moving to functional programming

James Fassett james at reggieband.com
Mon Jul 14 05:51:39 EDT 2008


On Jul 12, 12:18 am, George Sakkis <george.sak... at gmail.com> wrote:
> It relies on positional arguments, tuple unpacking and
> the signature of zip(),

It moreso relies on the fact that:

>>> t1 = (0,1,2,3)
>>> t2 = (7,6,5,4)
>>> [t1, t2] == zip(*zip(t1, t2))
True

This is mathematically true given the definition of zip. To me that is
very functional. Basically, unpacking a pair list into zip is the
canonical definition of unzipping the list (which is exactly my
intention).

> Second, it is less readable,

For a Python programmer - you are correct. For someone familiar with
the use of zip (as described above) - I wonder. Since I am new to this
I can't say for sure. If I posted the same code to a Haskell list or a
ML list would their opinion be the same?

> robust and efficient than the list comprehension.

I don't know the internals of how the Python interpreter treats list
comprehensions and zip but it seems reasonable to assume an extra list
is created for the zip approach.

However, in the limited functional code I have seen this is actually a
common practice. I would suppose in languages with lazy evaluation
this isn't a problem - but in Python it would be.

> The list comprehension is still the most pythonic approach though.

I agree that it is more Pythonic and preferable in this case.

Cheers,
James



More information about the Python-list mailing list