Functional schmunctional...

andrew cooke andrew at acooke.org
Tue Feb 10 15:54:51 EST 2009


r0g wrote:
> def ip2inet(a):
>   li = a.split('.')
>   assert len(li) == 4 or len(li) == 6
>   return reduce(add,[int(li[e])*(256**((len(li)-1)-e)) for e in
> xrange(0,len(li))])

what a mess.

i don't use this extreme a functional style in python (it's not really how
the language is intended to be used), but i think you can do better than
that.

how about:

from itertools import count

def ip2inet(a):
    blocks = a.split('.')
    assert len(blocks) in (4, 6)
    return sum(map(lambda (i, n): int(i) * 256**n,
                   zip(reversed(blocks), count(0))))

i haven't looked at the other function, but as a general comment it sounds
me like you are in such a hurry to point out fp is bad that you haven't
bothered to master it first.

andrew





More information about the Python-list mailing list