__getattribute__小例子

class student(object):
    def __init__(self,name=None,age=None):
        self.name = name
        self.age = age
    def __getattribute__(self, item):#getattribute方法修改类的属性
        if item == 'name':#如果为name属性名
            print("XiaoMing被我拦截住了")
            return "XiaoQiang " #返回值修改了name属性
        else:
            return object.__getattribute__(self,item)
    def show(self):
        print("姓名是: %s" %(self.name))
stu_one = student("XiaoMing",22)
print("学生姓名为:",stu_one.name)
print("学生年龄为:",stu_one.age)

'''
XiaoMing被我拦截住了
学生姓名为: XiaoQiang 
学生年龄为: 22
'''


2020-05-08

posted @ 2020-05-08 00:21  CodeYaSuo  阅读(116)  评论(0编辑  收藏  举报