Can I somehow refer to a method without using the lambda keyword?
Say we have following example code:
class AbstractDummy: def size(self): raise NotImplementedError class Dummy1(AbstractDummy): def size(self): return 10 class Dummy2(AbstractDummy): def size(self): return 20 If I have my example objects:
dummies1 = [Dummy1(), Dummy1(), Dummy1()] dummies2 = [Dummy2(), Dummy2()] Then if I want to map them, and I can do that with extracted function parameter to save me some characters:
f = lambda x : x.size() map(f, dummies1) map(f, dummies2) Question here: can I somehow avoid this temporary f and/or lambda keyword?
To make a small comparison, in Java it would be possible to refer to AbstractDummy::size and so the invocation would look a bit like print(map(AbstractDummy::size, dummies1).
AbstractDummy.sizebecause that will always call the abstract-classessizemethod (because in Python,.sizeis just a regular function here). So the lambda is a perfectly sensible solution. Alternatively, you could useoperator.methodcallerlambdaexpression to a name, just use adefstatement.map. Guido actually wanted to removemap/filter/reduceandlambdafrom Python 3! The only times I usemapare usually reserved for mapping simple built-in functions, sofor number in map(int, user_input)for example...some_instance.some_method is some_instance.some_methodwill be false!