MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/Python/comments/9jfko6/stop_writing_lambda_expressions_in_python/e6rm4i4/?context=3
r/Python • u/treyhunner Python Morsels • Sep 27 '18
26 comments sorted by
View all comments
Show parent comments
1
Can you explain whats going on with this lamda? Same as
def f(t, color):
where t is the 2-tuple (x,y)?
1 u/rcfox Sep 27 '18 No, it would be the same as:def f(point): (x, y), color = point return colorpoints is a list of tuples, and the key function only takes a single argument. Python 2.7 allows you to unpack an iterable in the place of a parameter. 2 u/__deerlord__ Sep 27 '18 Is it a list of 2-tuples? Can you provide an example of what point is, and then what it unpacks to for (x,y), color?Because the following works on py3c = [(1,2), (3,4)] (x,y), color = c x >>> 1 y >>> 2 color >>> (3,4) 1 u/rcfox Sep 27 '18 From the article: points = [((1, 2), 'red'), ((3, 4), 'green')]The thing that doesn't work in Python 3 is unpacking in the parameter list. ie:def foo(((x, y), color)): return color colors = map(foo, points)The above does work in Python 2.7 but not Python 3. 1 u/__deerlord__ Sep 28 '18 It sounds like this implicitly uses *args, but that in python3 you can use itertools.starmap for this. Explicit > implicit.Note: I havent played with this, though, too late here.
No, it would be the same as:
def f(point): (x, y), color = point return color
points is a list of tuples, and the key function only takes a single argument. Python 2.7 allows you to unpack an iterable in the place of a parameter.
points
key
2 u/__deerlord__ Sep 27 '18 Is it a list of 2-tuples? Can you provide an example of what point is, and then what it unpacks to for (x,y), color?Because the following works on py3c = [(1,2), (3,4)] (x,y), color = c x >>> 1 y >>> 2 color >>> (3,4) 1 u/rcfox Sep 27 '18 From the article: points = [((1, 2), 'red'), ((3, 4), 'green')]The thing that doesn't work in Python 3 is unpacking in the parameter list. ie:def foo(((x, y), color)): return color colors = map(foo, points)The above does work in Python 2.7 but not Python 3. 1 u/__deerlord__ Sep 28 '18 It sounds like this implicitly uses *args, but that in python3 you can use itertools.starmap for this. Explicit > implicit.Note: I havent played with this, though, too late here.
2
Is it a list of 2-tuples? Can you provide an example of what point is, and then what it unpacks to for (x,y), color?
Because the following works on py3
c = [(1,2), (3,4)] (x,y), color = c x >>> 1 y >>> 2 color >>> (3,4)
1 u/rcfox Sep 27 '18 From the article: points = [((1, 2), 'red'), ((3, 4), 'green')]The thing that doesn't work in Python 3 is unpacking in the parameter list. ie:def foo(((x, y), color)): return color colors = map(foo, points)The above does work in Python 2.7 but not Python 3. 1 u/__deerlord__ Sep 28 '18 It sounds like this implicitly uses *args, but that in python3 you can use itertools.starmap for this. Explicit > implicit.Note: I havent played with this, though, too late here.
From the article: points = [((1, 2), 'red'), ((3, 4), 'green')]
points = [((1, 2), 'red'), ((3, 4), 'green')]
The thing that doesn't work in Python 3 is unpacking in the parameter list. ie:
def foo(((x, y), color)): return color colors = map(foo, points)
The above does work in Python 2.7 but not Python 3.
1 u/__deerlord__ Sep 28 '18 It sounds like this implicitly uses *args, but that in python3 you can use itertools.starmap for this. Explicit > implicit.Note: I havent played with this, though, too late here.
It sounds like this implicitly uses *args, but that in python3 you can use itertools.starmap for this. Explicit > implicit.
Note: I havent played with this, though, too late here.
1
u/__deerlord__ Sep 27 '18
Can you explain whats going on with this lamda? Same as
where t is the 2-tuple (x,y)?