merge & de-duplicate lists

Alan Little contact at alanlittle.org
Wed Oct 8 11:17:50 EDT 2003


Alex wrote:

> > Two questions - passing the *name* of the method to call, and then
> > looking it up for each object in the list of extra sources (all of
> > which need to be new-style objects - not a problem in my application)
> > seems inelegant. My "sourcesOfAdditionalLists" are normally all of the
> > same class - is there something I can bind at class level that
> > automagically refers to instance-level attributes when invoked?
> 
> I'm not quite sure what you mean here.  You can of course play
> around with descriptors, e.g. properties or your own custom ones.
> But that 'normally' is worrisome -- what happens in the not-so-
> normal cases where one or two items are of a different class...?
 
what I was getting at was that I was thnínking I would like to be able
to call my function something like this (pseudocode):

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

Stashing the method name in a string is a level of indirection that
somehow felt to me like it *ought* to be unnecessary, but I think I
was just thinking in non-pythonic function pointer terms. Which
doesn't work in a world where (a) we don't know if two objects of the
same class have the same methods, and (b) we have the flexibility to
write functions that will work with anything that has a method of the
requisite name, regardless of its type.

Just learning out loud here (with the help of your Nutshell book, I
might add).




More information about the Python-list mailing list