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 会指向我们访问的属性名和方法名


 
                    
            
                

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-02-07
  • 2022-12-23
  • 2021-12-23
  • 2022-12-23
  • 2021-07-28
  • 2021-08-24
猜你喜欢
  • 2021-05-31
  • 2022-12-23
  • 2021-12-16
  • 2021-08-26
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案