Steven D'Aprano <[EMAIL PROTECTED]> wrote: > # Untested > def flattendict(d): > def gen(L): > return (x for M in exp(L) for x in rec(M)) > def exp(L): > return (L+list(kv) for kv in L.pop().iteritems()) > def rec(M): > return gen(M) if isinstance(M[-1],dict) else [M] > return dict((tuple(L[:-1]),L[-1]) for L in gen([d])) > > No. The function is hard to read, not because it uses lambdas, but > because it is obfuscated Python. The lambda syntax doesn't contribute > to the obfuscation.
In this particular case I think the lambda does contribute to the obfuscation. Yes, they are single expressions, but only because that have been contorted to become single expressions. The first two return generators, so if you don't force them into a lambda you can write them out as for loops with yield. The last one is an if..else statement. Splitting them into multiple statements also removes the need to keep them as short as possible, so you don't need to use single letter variable names everywhere. > And as for your point about bad code smells, no, I don't agree. If > your function consists of a single expression, and you don't expect > func.__name__ to have a meaningful value, then there's nothing wrong > with using a "named lambda". Anonymous functions are first-class > objects in Python, just as classes and modules and named functions > are, and people shouldn't make up superstitious rules about not > assigning them to names. You've got it backwards here: the lambdas were given names. If they hadn't been assigned to names I'd have no objection. -- http://mail.python.org/mailman/listinfo/python-list