zoukankan      html  css  js  c++  java
  • getattribute

    getattribute

    class Foo:
        def __init__(self,x):
            self.x=x
    
        def __getattr__(self, item):
            print('执行的是我')
            # return self.__dict__[item]
    
    f1=Foo(10)
    print(f1.x)
    f1.xxxxxx #不存在的属性访问,触发__getattr__
    class Foo:
        def __init__(self,x):
            self.x=x
    
        def __getattribute__(self, item):
            print('不管是否存在,我都会执行')
    
    f1=Foo(10)
    f1.x
    f1.xxxxxx  
    #_*_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
    
    #当__getattribute__与__getattr__同时存在,只会执行__getattrbute__,除非__getattribute__在执行过程中抛出异常AttributeError
    
    
  • 相关阅读:
    关于数论的一些总结
    gym101431B
    4.29训练题解
    hdu4347
    5.13训练的一些题解
    5.20训练的一些题解
    hdu4796
    hdu5984
    bzoj1941 hdu5992
    hdu4307
  • 原文地址:https://www.cnblogs.com/Manuel/p/12863580.html
Copyright © 2011-2022 走看看