Erik Max Francis wrote: > For instance, for a chat network bot framework, a certain form of bot > will look for any attribute in its instance that starts with verb_ and a > command and execute it when it hears it spoken: > > def verb_hello(self, convo): > "Respond to a greeting." > convo.respond(random.choice(self.greetings)) > > If you'd like it to respond to more than one word like this, then you > only need to assign the additional verbs, rather than redefine them: > > verb_hi = verb_hello > verb_yo = verb_hello > verb_wazzup = verb_hello
Well if you want these to work with subclasses that change verb_hello to do something else, one option is to write a simple decorator, and then your lines above become something like: verb_hi = caller(verb_hello) verb_yo = caller(verb_hello) verb_wazzup = caller(verb_hello) or if you prefer to only create one such function: _verb_hello_caller = caller(verb_hello) verb_hi = _verb_hello_caller verb_yo = _verb_hello_caller verb_wazzup = _verb_hello_caller Here's a simple example in action: py> def caller(func): ... def wrapper(self, *args, **kwargs): ... return getattr(self, func.__name__)(*args, **kwargs) ... return wrapper ... py> class C(object): ... def verb_hello(self): ... print "C: hello" ... verb_hi = caller(verb_hello) ... py> class D(C): ... def verb_hello(self): ... print "D: hello" ... py> D().verb_hi() D: hello Notice that verb_hi() still calls verb_hello() in the subclass. STeVe -- http://mail.python.org/mailman/listinfo/python-list