MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/Python/comments/iohrl/an_alternative_lambda_syntax_for_python/c25juux/?context=3
r/Python • u/vocalbit • Jul 13 '11
41 comments sorted by
View all comments
7
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.
-1 u/vocalbit Jul 13 '11 You bring up a good use case. It's not ideal, but you would have to write a def in this case. 1 u/aaronla Jul 14 '11 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, ... } 1 u/temptemptemp13 Jul 14 '11 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?
-1
You bring up a good use case. It's not ideal, but you would have to write a def in this case.
1 u/aaronla Jul 14 '11 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, ... } 1 u/temptemptemp13 Jul 14 '11 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?
1
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, ... }
1 u/temptemptemp13 Jul 14 '11 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?
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?
7
u/Liorithiel Jul 13 '11 edited Jul 13 '11
What will you do when you want to put a lambda function in a list? Example:
This is great when parsing things like csv tables or whatever, so it is actually useful.