Lambda question

rusi rustompmody at gmail.com
Mon Jun 6 13:29:40 EDT 2011


On Jun 5, 11:33 pm, Terry Reedy <tjre... at udel.edu> wrote:
> On 6/5/2011 5:31 AM, Alain Ketterlin wrote:
>
> > <jyoun... at kc.rr.com>  writes:
>
> >>>>> f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc
>
> f=lambda ... statements are inferior for practical purposes to the
> equivalent def f statements because the resulting object is missing a
> useful name attribute and a docstring. f=lambda is only useful for
> saving a couple of characters, and the above has many unneeded spaces
>
>
>
> >>>>> f("Hallo Welt", 3)
> >> ['Hal', 'lo ', 'Wel', 't']
>
> >>http://stackoverflow.com/questions/312443/how-do-you-split-a-list-int...
> >> ized-chunks-in-python/312644
>
> >> It doesn't work with a huge list, but looks like it could be handy in certain
> >> circumstances.  I'm trying to understand this code, but am totally lost.
>
> > With such dense code, it is a good idea to rewrite the code using some
> > more familiar (but equivalent) constructions. In that case:
>
> > f =<a function that can be called with parameters>  x, n, acc=[]:
> >        <if>  x<is not empty>
> >          <result-is>  f(x[n:], n, acc+[(x[:n])])
> >        <else>
> >          <result-is>  acc
>
> Yes, the following is much easier to read:
>
> def f(x, n, acc=[]):
>    if x:
>      return f(x[n:], n, acc + [x[:n]])
>    else:
>      return acc
>
> And it can be easily translated to:
>
> def f(x,n):
>    acc = []
>    while x:
>      acc.append(x[:n])  # grab first n chars
>      x = x[n:]          # before clipping x
>    return acc
>
> The repeated rebinding of x is the obvious problem. Returning a list
> instead of yielding chunks is unnecessary and a problem with large
> inputs. Solving the latter simplies the code to:
>
> def group(x,n):
>    while x:
>      yield x[:n]  # grab first n chars
>      x = x[n:]    # before clipping x
>
> print(list(group('abcdefghik',3)))
> # ['abc', 'def', 'ghi', 'k']
>
> Now we can think about slicing chunks out of the sequence by moving the
> slice index instead of slicing and rebinding the sequence.
>
> def f(x,n):
>      for i in range(0,len(x),n):
>          yield x[i:i+n]
>
> This is *more* useful that the original f= above and has several *fewer*
> typed characters, even is not all on one line (and decent editor add the
> indents automatically):


>
> def f(x,n): for i in range(0,len(x),n): yield x[i:i+n]
> f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc

Well here is a quite-readable one-liner
def f(x,n): return (x[i:i+n] for i in range(0,len(x),n))

which if one is in character-lessening-spree mode can be written:

f=lambda x,n: (x[i:i+n] for i in range(0,len(x),n))

> Let me add something not said much here about designing functions: start
> with both a clear and succinct definition *and* test cases. (I only
> started writing tests first a year ago or so.)

I am still one year in the future :-;
Which framework do you recommend? Nose? test.py?



More information about the Python-list mailing list