属性访问拦截器
class Itcast(object): def __init__(self,subject1): self.subject1 = subject1 self.subject2 = "go" #属性访问拦截器,打log def __getattribute__(self, item): print("---1>%s" %item) if item == "subject1": print("log subject1") return 'redirect python' else: temp = object.__getattribute__(self,item) print("---2>%s"%str(temp)) return temp def show(self): print("this is Itcast") s = Itcast("python") print("--------1--------") print(s.subject1) print("--------2--------") print(s.subject2) print("--------3--------") s.show() print("--------4--------")
输出
--------1-------- ---1>subject1 log subject1 redirect python --------2-------- ---1>subject2 ---2>go go --------3-------- ---1>show ---2><bound method Itcast.show of <__main__.Itcast object at 0x000001F47161A860>> this is Itcast --------4--------