Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

Dr.Professor

  • 3 years ago

How do I apply a method to a sequence of objects?

  • This Question is Closed
  1. Rohangrr
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Use a list comprehension: result = [obj.method() for obj in mylist] More generically, you can try the following function: def method_map(objects, method, arguments): """method_map([a,b], "meth", (1,2)) gives [a.meth(1,2), b.meth(1,2)]""" nobjects = len(objects) methods = map(getattr, objects, [method]*nobjects) return map(apply, methods, [arguments]*nobjects)

  2. Dr.Professor
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Thanks

  3. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy