If the contents of the list are all instances of the same class, you can prefix the method name with the class name.
class Fred: def __init__(self, val): self.val = val def frob(self): return self.val freds = [Fred(4), Fred(8), Fred(15)] print map(Fred.frob, freds)
Result:
[4, 8, 15]
This can also be done if the list items are subclasses of the specified class. However, it will still invoke the specified method implementation, even if that method is overridden in a subclass. Example:
class Fred: def __init__(self, val): self.val = val def frob(self): return self.val class Barney(Fred): def frob(self): return self.val * 2 freds = [Fred(4), Barney(8), Barney(15)]
source share