zoukankan      html  css  js  c++  java
  • 面向对象——类的内置attr(三十三)

    class Foo:
        x=1
        def __init__(self,y):
            self.y=y
    
        def __getattr__(self, item):
            print('----> from getattr:你找的属性不存在')
    
    
        def __setattr__(self, key, value):
            print('----> from setattr')
            # self.key=value #这就无限递归了,你好好想想
            # self.__dict__[key]=value #应该使用它
    
        def __delattr__(self, item):
            print('----> from delattr')
            # del self.item #无限递归了
            self.__dict__.pop(item)
    
    #__setattr__添加/修改属性会触发它的执行
    f1=Foo(10)
    print(f1.__dict__) # 因为你重写了__setattr__,凡是赋值操作都会触发它的运行,你啥都没写,就是根本没赋值,除非你直接操作属性字典,否则永远无法赋值
    f1.z=3
    print(f1.__dict__)
    
    #__delattr__删除属性的时候会触发
    f1.__dict__['a']=3#我们可以直接修改属性字典,来完成添加/修改属性的操作
    del f1.a
    print(f1.__dict__)
    
    #__getattr__只有在使用点调用属性且属性不存在的时候才会触发
    f1.xxxxxx
    
    三者的用法演示
    class Foo:
        def __init__(self,x):
            self.x = x
    
        def __getattr__(self, item):
            print("执行__getattr__")
    
        def __delattr__(self, item):
            print("执行 __delattr__")
            self.__dict__.pop(item)
        def __setattr__(self, key, value):
            print("执行__setattr__")
            self.__dict__[key] = value
    
    f1 = Foo(10) # 执行__setattr__
    print(f1.x) # 10
    f1.dfhsjdsfh # 执行__getattr__ 调用f1 中不存在的属性是会触发
    
    print(f1.__dict__) # {'x': 10}
    f1.y = 3 # 执行__setattr__
    print(f1.__dict__) # {'y': 3, 'x': 10}
    
    f1.__dict__['z'] = 5
    print(f1.__dict__) # {'x': 10, 'y': 3, 'z': 5}
    del f1.z # 执行 __delattr__
    print(f1.__dict__) # {'x': 10, 'y': 3}
  • 相关阅读:
    iframe引入网页
    input同名
    混合框架
    <header><footer>引用
    <dl>
    凸包性质——cf1044C
    几何求叉积+最短路——cf1032D
    fresco 设置资源路径时的一个坑
    马拉车+贪心——cf1326D
    【模板变形】凸壳二分+斜率优化dp——cf1083E
  • 原文地址:https://www.cnblogs.com/xiangtingshen/p/10461845.html
Copyright © 2011-2022 走看看