zoukankan      html  css  js  c++  java
  • __getattribute__

    getattribute

    一、getattr

    • 不存在的属性访问,触发__getattr__
    class Foo:
        def __init__(self, x):
            self.x = x
    
        def __getattr__(self, item):
            print('执行的是我')
            # return self.__dict__[item]
    
    
    f1 = Foo(10)
    
    print(f1.x)
    
    10
    
    f1.xxxxxx
    
    执行的是我
    

    二、getattribute

    查找属性无论是否存在,都会执行

    undefined

    你可真霸道呀!!!

    class Foo:
        def __init__(self, x):
            self.x = x
    
        def __getattribute__(self, item):
            print('不管是否存在,我都会执行')
    
    f1 = Foo(10)
    
    f1.x
    
    不管是否存在,我都会执行
    
    f1.xxxxxx
    
    不管是否存在,我都会执行
    

    三、getattr__与__getattribute

    当__getattribute__与__getattr__同时存在,只会执行__getattrbute__,除非__getattribute__在执行过程中抛出异常AttributeError

    #_*_coding:utf-8_*_
    __author__ = 'Linhaifeng'
    
    
    class Foo:
        def __init__(self, x):
            self.x = x
    
        def __getattr__(self, item):
            print('执行的是我')
            # return self.__dict__[item]
        def __getattribute__(self, item):
            print('不管是否存在,我都会执行')
            raise AttributeError('哈哈')
    
    
    f1 = Foo(10)
    
    f1.x
    
    不管是否存在,我都会执行
    执行的是我
    
    f1.xxxxxx
    
    不管是否存在,我都会执行
    执行的是我
    
  • 相关阅读:
    4. ConcurrentHashMap 锁分段机制
    3. 原子变量-CAS算法
    2. 原子变量
    1. volatale 关键字 -内存可见性
    6.8 全局查询日志
    js实现数字分页
    拆箱和装箱
    string与stringbuilder的区别
    C#之out与ref的共性与区别以及用法
    asp.net操作xml(增删查改)
  • 原文地址:https://www.cnblogs.com/Dr-wei/p/11851095.html
Copyright © 2011-2022 走看看