Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call same method for a list of objects?

Tags:

python

Suppose code like this:

class Base:     def start(self):         pass     def stop(self)         pass  class A(Base):     def start(self):         ... do something for A     def stop(self)         .... do something for A  class B(Base):     def start(self):      def stop(self):  a1 = A(); a2 = A() b1 = B(); b2 = B()  all = [a1, b1, b2, a2,.....] 

Now I want to call methods start and stop (maybe also others) for each object in the list all. Is there any elegant way for doing this except of writing a bunch of functions like

def start_all(all):     for item in all:         item.start()  def stop_all(all): 
like image 631
Dmitry Avatar asked Apr 21 '10 10:04

Dmitry


People also ask

How do you call methods of objects?

Calling an object's method is similar to getting an object's variable. To call an object's method, simply append the method name to an object reference with an intervening '. ' (period), and provide any arguments to the method within enclosing parentheses.

What is the __ call __ method?

The __call__ method enables Python programmers to write classes where the instances behave like functions and can be called like a function.


2 Answers

This will work

all = [a1, b1, b2, a2,.....]  map(lambda x: x.start(),all)     

simple example

all = ["MILK","BREAD","EGGS"] map(lambda x:x.lower(),all) >>>['milk','bread','eggs'] 

and in python3

all = ["MILK","BREAD","EGGS"] list(map(lambda x:x.lower(),all)) >>>['milk','bread','eggs'] 
like image 199
Mark Essel Avatar answered Oct 08 '22 04:10

Mark Essel


It seems like there would be a more Pythonic way of doing this, but I haven't found it yet.

I use "map" sometimes if I'm calling the same function (not a method) on a bunch of objects:

map(do_something, a_list_of_objects) 

This replaces a bunch of code that looks like this:

 do_something(a)  do_something(b)  do_something(c)  ... 

But can also be achieved with a pedestrian "for" loop:

  for obj in a_list_of_objects:        do_something(obj) 

The downside is that a) you're creating a list as a return value from "map" that's just being throw out and b) it might be more confusing that just the simple loop variant.

You could also use a list comprehension, but that's a bit abusive as well (once again, creating a throw-away list):

  [ do_something(x) for x in a_list_of_objects ] 

For methods, I suppose either of these would work (with the same reservations):

map(lambda x: x.method_call(), a_list_of_objects) 

or

[ x.method_call() for x in a_list_of_objects ] 

So, in reality, I think the pedestrian (yet effective) "for" loop is probably your best bet.

like image 27
abonet Avatar answered Oct 08 '22 05:10

abonet