[Python] How to use map() to call methods on a list of objects

Discussão em 'Python' iniciado por Stack, Abril 25, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to call object.method() on a list of objects.

    I have tried this but can't get it to work properly

    newList = map(method, objectList)


    I get the error method is not defined but I know that is because it is a method of the objects and not a local function.

    Is there a way to do this with map(), or a similar built in function? Or will I have to use a generator/list comprehension?

    Could you also explain the advantages or contrast your solution to using this list comprehension?

    newList = [object.method() for object in objectList]

    Continue reading...

Compartilhe esta Página