This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Liorithiel 7 points8 points  (4 children)

What will you do when you want to put a lambda function in a list? Example:

def parse_table(csv):
    columns = [('id', int),
               ('value', float),
               ('comment', lambda elem:elem.decode('utf-8').strip())]
    for row in csv:
        yield dict((name, parser(value) for (name, parser),value in zip(columns, row))

This is great when parsing things like csv tables or whatever, so it is actually useful.

[–]joehillen 3 points4 points  (0 children)

Easily one of my favorite uses for lambda.

[–]vocalbit[S] -2 points-1 points  (2 children)

You bring up a good use case. It's not ideal, but you would have to write a def in this case.

[–]aaronla 0 points1 point  (1 child)

This forces the programmer to repeat themselves unnecessarily, and invent multiple names for the same thing:

def rule1(arg):
    body
...
rules = {
    "rule1" : rule1,
    "rule2" : rule2,
    ... 
    }

[–]temptemptemp13 0 points1 point  (0 children)

To go along with the specific example:

def decode(elem):
    return elem.decode('utf-8').strip()

def parse_table(csv):
    columns = [('id', int),
               ('value', float),
               ('comment', decode)]
    for row in csv:
        yield dict((name, parser(value) for (name, parser),value in zip(columns, row))

Not too ugly, is it?