Use python decorators on class methods and subclass methods

Posted by AlexH on Stack Overflow See other posts from Stack Overflow or by AlexH
Published on 2010-04-26T04:04:03Z Indexed on 2010/04/26 4:13 UTC
Read the original article Hit count: 552

Filed under:
|
|

Goal: Make it possible to decorate class methods. When a class method gets decorated, it gets stored in a dictionary so that other class methods can reference it by a string name.

Motivation: I want to implement the equivalent of ASP.Net's WebMethods. I am building this on top of google app engine, but that does not affect the point of difficulty that I am having.

How it Would look if it worked:

class UsefulClass(WebmethodBaseClass):
def someMethod(self, blah): print(blah) @webmethod def webby(self, blah): print(blah) # the implementation of this class could be completely different, it does not matter # the only important thing is having access to the web methods defined in sub classes class WebmethodBaseClass(): def post(self, methodName): webmethods[methodName]("kapow") ... a = UsefulClass() a.post("someMethod") # should error a.post("webby") # prints "kapow"

There could be other ways to go about this. I am very open to suggestions

© Stack Overflow or respective owner

Related posts about python

Related posts about decorators