动态语言

动态语言提供了在运行时改变程序结构的能力。有些人认为这会造成混乱,不过我觉得,这些功能有时候会提供巨大的方便。

这里举个例子。函数调用拦截是aop的基础,用python实现这个功能非常简单。

#拦截的目标
class Target:
    def targetFunc(self):
        print “targetFunction”

#拦截
temp=Target.targetFunc
def foo(self):
    print “before call”
    temp(self)
    print “after call”
Target.targetFunc=foo

#看看结果
t=Target()
t.targetFunc()

对调用者来说根本就不知道函数已经给动了手脚。
这个要是用Java, C++来实现就麻烦大了。

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s