目标:使装饰类方法成为可能。当一个类方法被装饰时,它被存储在一个字典中,以便其他类方法可以通过字符串名称引用它。在类方法和子类方法上使用python装饰器
动机:我想实现ASP.Net的WebMethods的等价物。我正在谷歌应用程序引擎之上构建这个,但这并不影响我所遇到的难点。
会如何看待它的工作:
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"
可能有其他的方式去了解这一点。我非常乐于提供建议