首页 > 其他 > 详细

__getattriute__

时间:2019-08-18 13:20:24      阅读:75      评论:0      收藏:0      [点我收藏+]
# class Foo:
#     def __init__(self,x):
#         self.x = x
#     def __getattr__(self,item):
#         print("__getattr__")
#         # return self.__dict__[item]
#     def printer(self):
#         print("lsdajfl")
#
# f1 = Foo(10)
# print(f1.x)
# f1.abc # 访问不存在的属性,触发__getattr__
# f1.printer()
# class Foo1:
#     def __init__(self,x):
#         self.x = x
#     def __getattribute__(self,item):
#         print("__getattribute__")
# # 由上边的例子我们可以看出,不管属性存在不存在都会执行
# # __getattribute__
# f1 = Foo1(10)
# print(f1.x)
# print(f1.abc)

class Foo2:
    def __init__(self,x):
        self.x = x
    def __getattr__(self,item):
        print(执行__getattr__)
    def __getattribute__(self,item):
        print("执行__getattribute__")
    def printer(self):
        print(123)

# 当两个都存在的时候,只会执行__getattribute__
# 而不执行__getattr__
f2 = Foo2(20)
f2.x
f2.xxx
# f2.printer()
#  当我们修改了__getattribute__方法时,本来有的方法也无法执行了

 

__getattriute__

原文:https://www.cnblogs.com/cong12586/p/11371942.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!