zoukankan      html  css  js  c++  java
  • 16 __getattr__ 与 __setattr__

    __getattr__

    官方文档

    • Python 3.7.3
    object.__getattr__(self, name)
    
        Called when the default attribute access fails with an AttributeError (either __getattribute__() raises an AttributeError because name is not an instance attribute or an attribute in the class tree for self; or __get__() of a name property raises AttributeError). This method should either return the (computed) attribute value or raise an AttributeError exception.
    
        Note that if the attribute is found through the normal mechanism, __getattr__() is not called. (This is an intentional asymmetry between __getattr__() and __setattr__().) This is done both for efficiency reasons and because otherwise __getattr__() would have no way to access other attributes of the instance. Note that at least for instance variables, you can fake total control by not inserting any values in the instance attribute dictionary (but instead inserting them in another object). See the __getattribute__() method below for a way to actually get total control over attribute access.
    

    举例

    例1

    >>> class A(object):
    ...     pass
    ...
    >>> a = A()
    >>> a.name
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'A' object has no attribute 'name'
    >>> 
    

    例2

    >>> class A(object):
    ...     def __getattr__(self, name):
    ...         print("no way, haha")
    ...
    >>> a = A()
    >>> a.name
    no way, haha
    >>> 
    

    小结

    • 实例对象进行“点操作”时,会自动调用 __getattr__()

    __setattr__

    官方文档

    • Python 3.7.3
    object.__setattr__(self, name, value)
    
        Called when an attribute assignment is attempted. This is called instead of the normal mechanism (i.e. store the value in the instance dictionary). name is the attribute name, value is the value to be assigned to it.
    
        If __setattr__() wants to assign to an instance attribute, it should call the base class method with the same name, for example, object.__setattr__(self, name, value).
    

    举例

    例1

    class A(object):
        def __init__(self, num):
            self.num = num
    
    
    a = A(20)
    print(a.num)
    
    a.num = 30
    print(a.num)
    
    a.__setattr__('num', 40)
    print(a.num)
    

    >>>

    20
    30
    40
    

    例2

    class A(object):
        def __setattr__(self, num, value):
            print("--setattr--")
            # self.num = value  # 死循环
            super().__setattr__(num, value)
    
    
    a = A()
    
    print('1.')
    a.num = 18
    print('2.')
    print(a.num)
    

    >>>

    1.
    --setattr--
    2.
    18
    
  • 相关阅读:
    bert中的为什么
    ROS+Ubuntu+VSCode
    致我成为社畜的第一年
    SQL实战 11.高频SQL面试题 课程订单分析系列
    SQL实战 10.高频SQL面试题 考试分数系列
    SQL实战 9.高频SQL面试题 网站访客最近登录日期系列
    SQL实战 8.异常的邮件概率 sum、count、round和case when结合
    SQL实战 7刷题通过的题目排名 row_number、 dense_rank 和rank区别
    SQL实战 6.对于employees表中,给出奇数行的first_name
    SQL实战 5.统计salary的累计和running_total
  • 原文地址:https://www.cnblogs.com/yorkyu/p/10693197.html
Copyright © 2011-2022 走看看