Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Not sure if this does what you want or not:</p> <pre><code>class CollectionOfThings(object): def __init__(self): self.list_of_objects = [] self.list_of_objects = [MultipleThings(index) for index in range(5)] self.callFunc(self.list_of_objects, "some_function") def callFunc(self, items, func, *args, **kwargs): for thing in items: getattr(thing, func)(*args, **kwargs) </code></pre> <p>As @Thorsten points out, simply using <code>map</code> with a <code>lambda</code> is a way to do what you are trying to do. My attempt was to make a general solution.</p> <p>You could pass the function itself like so to avoid passing a string argument.</p> <pre><code>class CollectionOfThings(object): def __init__(self): self.list_of_objects = [] self.list_of_objects = [MultipleThings(index) for index in range(5)] self.callFunc(self.list_of_objects, MultipleThings.some_function) def callFunc(self, items, func, *args, **kwargs): for thing in items: func(thing, *args, **kwargs) </code></pre> <p>This returns both a list and dict:</p> <pre><code>class MultipleThings(object): def __init__(self, index): self.index = index def some_function(self, key, value): return "I am index %s with key %s and value %s" % (self.index, key, value) class CollectionOfThings(object): def __init__(self): self.list_of_objects = [] self.list_of_objects = [MultipleThings(index) for index in range(5)] retList, retDict = self.callFunc(self.list_of_objects, MultipleThings.some_function, "foo", value="bar") print retList, retDict def callFunc(self, items, func, *args, **kwargs): retList = [] retDict = {} for i, thing in enumerate(items): funcReturn = func(thing, *args, **kwargs) retList.append(funcReturn) retDict[i] = funcReturn return retList, retDict </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload