Binding first parameter of method to constant value
Johannes Bauer
dfnsonfsduifb at gmx.de
Fri May 18 08:14:33 EDT 2012
Hi group,
I'm trying to dynamically add methods to a class at runtime. What I
would like to do is have them all delegated to one common function which
is called with the name of the function as first parameter. I.e.:
class Foo():
def __init__(self):
# Some magic missing here
setattr(self, "foometh", types.MethodType(self._dispatcher, self))
setattr(self, "barmeth", types.MethodType(self._dispatcher, self))
def _dispatcher(self, caller, *args):
# Dispatcher called with caller == "foometh"
# or caller == "barmeth", depending on which one was called
with the effect that
f = Foo()
f.foometh(1, 2, 3)
# -> this should be equivaent to Foo._dispatcher(f, "foometh", 1, 2, 3)
f.barmeth()
# -> this should be equivaent to Foo._dispatcher(f, "barmeth")
I'm kind of stuck. Can you please give me hints?
Best regards,
Joe
--
>> Wo hattest Du das Beben nochmal GENAU vorhergesagt?
> Zumindest nicht öffentlich!
Ah, der neueste und bis heute genialste Streich unsere großen
Kosmologen: Die Geheim-Vorhersage.
- Karl Kaos über Rüdiger Thomas in dsa <hidbv3$om2$1 at speranza.aioe.org>
More information about the Python-list
mailing list