4 ms·
> I like lambda a, b: a + b syntax better than lambda a_b: a_b[0] + a_b[1] It already works this way. Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52)
by recursive 10y ago
> I like lambda a, b: a + b syntax better than lambda a_b: a_b[0] + a_b[1]
It already works this way.
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> add = lambda a, b: a + b
>>> add(1, 2)
3
> I prefer map/reduce/filter to return lists rather than iterable
You can produce a list from any iterable by passing it to `list()`. You cannot take a materialized list and make it lazy though.
> I prefer dict.keys/values/items to return sets/lists rather than iterables, unless I call dict.iter[keys/values/items]
Why? Sets are mutable. What happens when you mutate dict.values? It doesn't make sense.
- Marat_Dukhan 10y agolambda a, b: a + b kind of works, but you can't call it with tuple argument (which you could in Python 2.7). This hurts when e.g. you have a list of pairs and try to map when via lambda, e.g.: map(lambda a, b: a+b, [(1, 2), (3, 4)]). Even worse, this won't fail right away in Python 3 (thanks, lazy evaluation in map), but will raise when you try to use the result of map
- ben-schaaf 10y agoThat doesn't work in the python 2.7 that I'm running, just like python 3 it complains that I don't have enough arguments.
- Marat_Dukhan 10y agoRight, needs brackets: lambda (a, b)
- recursive 10y agoAs far as I can tell, you're complaining about one character. This works. >>> add = lambda a, b: a + b >>> t = (1, 2) >>> add(*t) 3 Is that extra asterisk the problem? Again, if you want a materialized list, pass it to `list()`. Projection failures will raise then.
- Marat_Dukhan 10y agoYes, sets are mutable, but if I call dict.keys, I expect to get a copy of keys.