zoukankan      html  css  js  c++  java
  • Effective Python Ver2.0_StudyNotes___getattr__、__getattribute__及__setattr__的一些知识点

    __getattr__:每当访问类对象的属性,此时实例字典又找不到该属性时,触发__getattr__,简言之:访问实例化对象没有的属性时触发
    __getattribute__:主要访问对象中的属性就会触发此方法
    __setattr__:只要给对象进行属性赋值操作就会触发此方法,无论是直接赋值还是通过setattr函数赋值

    这里有个技巧需要知道,如果想要访问属性或者设置属性赋值的过程不触发 __ ** __ 方法,可以使用super().__getattr__('name')super().__getattribute__('name')super().__setattr__('key', 'value')来处理。

    示例:

    class DictionaryRecord:
        def __init__(self, data):
            self._data = data
            self.a = 1
    
        def __getattribute__(self, item):
            print(f"call __getattribute__({item !r})")
            data_dict = super().__getattribute__('_data')  # 超类的__getattribute__方法是直接从实例的属性字典获取的,不会触发__getattritube__
            return data_dict[item]
    
        def __setattr__(self, key, value):
            print(f"__setattr__ is run...")
            print(key, value)
            super().__setattr__(key, value)
    
    data = DictionaryRecord({'foo': 3})
    print(data.foo)
    

    Result:
    setattr is run...
    _data {'foo': 3}
    setattr is run...
    a 1
    call getattribute('foo')
    3

    Tips:

    "Harold's a clever {0!s}"        # Calls str() on the argument first
    "Bring out the holy {name!r}"    # Calls repr() on the argument first
    "More {!a}"                      # Calls ascii() on the argument first
    

    format-string-syntax

    要点总结
    1、如果想要用自己的方式(例如惰性地或按需地)加载并保存对象属性,那么可以在该对象所属的类里实现__getattr____setattr__特殊方法
    2、__getattr__只会在属性缺失时触发,而__getattribute__则在每次访问属性时都要触发
    3、在实现__getattribute____setattr__的过程中,如果要使用本对象的普通属性,那么应该通过super()(也就是object类)来使用,而不要直接使用,以避免无限递归


    请相信自己

    当我们迷茫,懒惰,退缩的时候 我们会格外的相信命运 相信一切都是命中注定

    而当我们努力拼搏,积极向上时 我们会格外的相信自己

    所以命运是什么呢? 它是如果你习惯它 那它就会一直左右你

    如果你想挣脱它 那它就成为你的阻碍 可如果你打破了它 那它就是你人生的垫脚石!


    如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!

  • 相关阅读:
    poj2386 Lake Counting
    poj 1852 Ants
    Luogu P2419 [USACO08JAN]牛大赛Cow Contest
    Luogu P2336 [SCOI2012]喵星球上的点名
    Luogu P2463 [SDOI2008]Sandy的卡片
    Luogu P2852 [USACO06DEC]牛奶模式Milk Patterns
    Luogu P4248 [AHOI2013]差异
    【NOI2008】志愿者招募
    Luogu P2743 [USACO5.1]乐曲主题Musical Themes
    P3723 [AH2017/HNOI2017]礼物
  • 原文地址:https://www.cnblogs.com/suguangti/p/15236178.html
Copyright © 2011-2022 走看看