merge & de-duplicate lists
anton muhin
antonmuhin.REMOVE.ME.FOR.REAL.MAIL at rambler.ru
Wed Oct 8 11:37:32 EDT 2003
Alan Little wrote:
> def f (listOfObjects, method) :
> for o in listOfObjects :
> o.method # passed method gets invoked on the instances
>
> l = [a,b,c] # collection of objects of known class C
> result = f(l, C.method) # call with method of the class
>
Maybe the following might interest you:
1.
class A(object):
def fooA(self):
return "fooA"
class B(object):
def fooB(self):
return "fooB"
a, b = A(), B()
invoke_fooA = lambda obj: obj.fooA()
invoke_fooB = lambda obj: obj.fooB()
print invoke_fooA(a)
ptin invoke_fooB(b)
2. If the class is the same you can use:
class A(object):
def foo(self):
return "A.foo"
method = A.foo
a = A()
print method(a)
HTH,
anton.
More information about the Python-list
mailing list