Map/Iterating through List of Python Object

Is there a way to iterate and call a function on a list of user-defined Python objects with multiple attributes? Let's suppose it's called Entry, with attribute name, and age.

Such that I can say something to the effect of

def func(name, age):
    //do something

def start(list_of_entries)
    map(func, list_of_entries.name(), list_of_entries.age()) 
    //but obviously the .name and .age of the object, not the iterable
    //these are the only two attributes of the class

Was thinking about using functools.partial() but not sure if that is even valid in this case.

Answers


I suppose you could use a lambda function:

>>> def start(list_of_entries):
...     map((lambda x:func(x.name,x.age)), list_of_entries)

But why not just use a loop?:

>>> def start(list_of_entries):
...     for x in list_of_entries: func(x.name, x.age)

or if you need the results of func:

>>> def start(list_of_entries):
...     return [func(x.name, x.age) for x in list_of_entries]

Need Your Help

Toggle button on table header works only after 2 toggles

javascript jquery css jquery-datatables

I have a table with search functionality, where the input to search is opened in the table column headers when clicking the toggle button. When clicking the button again, it reverts back to column ...

Need to extract XML document from NSMutableData object in iOS

ios nsurlconnection nsmutableurlrequest nsmutabledata

I am successfully connecting to a .NET web service from iOS and print the response xml document to the console via NSLog. This I am doing via the AFNetworking Library. I am now able to connect to...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.