class Itcast(object):
def __init__(self, subject1):
self.subject1 = subject1
print("^^^^^^^-------%s" %self.subject1)
self.subject2 = ‘cpp‘
def __getattribute__(self, obj):
print("===========1============")
print("-------%s" %obj)
if obj == ‘subject1‘:
print(‘log subject1‘)
return ‘redirect python‘
else:
return object.__getattribute__(self,obj)
def show(self):
print(‘this is Itcast‘)
s = Itcast(‘python‘)
#print(s.subject1)
s.show()
#print(s.subject2)
===============================================
===========1============
-------subject1
log subject1
^^^^^^^-------redirect python
===========1============
-------show
this is Itcast
这个方法是在我们访问类的属性或方法时自动调用(在我们访问属性前调用)
这个方法中有个参数obj 会指向我们访问的属性名和方法名