more on "Is this considered black magic?"

Marcin 'Qrczak' Kowalczyk qrczak at knm.org.pl
Mon Dec 3 07:03:37 EST 2001


3 Dec 2001 10:52:17 GMT, Philip Swartzleonard <starx at pacbell.net> pisze:

> Hm, is there something sane that can be done to make 'method' in this 
> situation a variable? The way i'm designing things, it would be useful to 
> have a 'call ArbitrayFunction of all X in container' function.

I don't see how to make it simpler in Python. This looks less clear
than a direct 'for' loop IMHO:

def call_method_for(objects, method, *args):
    for o in objects:
        getattr(o, method)(*args)

call_method_for(objects, 'method', args)

There is little to abstract away because there is no good way to express
"a method to call". Another approach is to express it with a function:

def call_for(objects, f):
    for o in objects:
        f(o)

call_for(objects, lambda o: o.method(args))

Not an improvement either.

-- 
 __("<  Marcin Kowalczyk * qrczak at knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^
QRCZAK



More information about the Python-list mailing list