3 ms·
To me your version is less clearer than the explicit if calls: * Name 'map' for a variable is a poor choice (as it has same name as the python builtin funct
by sateesh 14y ago
To me your version is less clearer than the explicit if calls:
* Name 'map' for a variable is a poor choice (as it has same
name as the python builtin function map)
* Your version has off by one error, it doesn't give correct results when called with a single element list or if the list has three elements:
>>>mymap = [
lambda args: (0, args[0], 1),
lambda args: (args[0], args[1], 1),
lambda args: args,
]
>>>args = [5]
>>>mymap[len(args)](args)
IndexError
Traceback (most recent call last)
....
# This shouldn't be the case, a list with a single
# element is a valid input
>>>args = [1,6,1]
>>>mymap[len(args)](args)
IndexError Traceback (most recent call last)
...
# This shouldn't be the case, a list with a three
# elements is a valid input
- saintfiends 14y agoIf only lambda's allowed statements :(. >>>mymap = [ lambda args: raise IndexError, lambda args: (0, args[0], 1), lambda args: (args[0], args[1], 1), lambda args: args, ] That aside, this approach would be much slower too.