• __getattribute__


    在类 里面,其实并没有方法这个东西,所有的东西都保存在属性里面,所谓的调用方法其实是类里面的一个同名属性指向了一个函数(方法),返回的是函数的引用,再用   函数()    这种方式就可以调用它 

    在调用实例的方法的时候,实际上给对象传过去的只是一个字符串而已,

    比如 t = Test() , 假设 func 是 Test类的一个方法,那么 t.func() 是先给类的 __getattribute__ 方法传过去了一个 'func' 字符串 ,经过处理之后返回的是指向 func 函数的引用 ,最后 func() 就调用了这个函数(方法)

    class Test:
    
        def __init__(self):
            self.a = 100
            self.b = 200
    
        def __getattribute__(self, item):
            if item == 'a':
                print('------%s------' % item)
                return '%s is get' % item
            else:
                return object.__getattribute__(self, item)
    
    t = Test()
    print(t.a)
    
    >>>------a------
       a is get

    无论调用属性还是方法,都是先强制调用 __getattribute__ 方法,然后再传回属性的值或者是 函数(方法)的引用.

    注意,在 __getattribute__ 方法里面 如果再用类似 self.*** 这种调用方式 ,那么是一件很不好的行为,因为 每次调用类的属性都会强制调用 __getattribute__ ,所以极有可能造成递归调用.如:

    class Test:
    
        def __getattribute__(self, item):
            if item == 'a':
                print('------%s------' % item)
                return '%s is get' % item
            else:
                return self.test  # <----------------------------------------------------------------------------
    
        def test(self):
            print('.........')
    
    t = Test()
    print(t.b)
    
    >>>RecursionError: maximum recursion depth exceeded in comparison

    self.test 会再次调用 __getattribute__ , 最后结果 就是 递归异常:最大递归深度超过什么什么....

    在类的magic方法里面还有一个 __getattr__ 方法,这个方法是归属于 _getattribute__ 调用的,

    就是 访问属性的时候首先会调用 __getattribute__ 方法,这个方法会检测 __dict__ 里面有没有这个属性,没有的话最后再调用一下 __getattr__

    总结: 首先必须调用 __getattribute__ , 里面实现了 查找 __dict__ 里面有没有这个键,如果没有再调用 __getattr__方法.

  • 相关阅读:
    Problem of saving images in WPF (RenderTargetBitmap)zz
    巴特沃斯(Butterworth)滤波器 (2)
    巴特沃斯(Butterworth)滤波器 (1)
    vs发布的程序不依赖运行时库msvcp100.dll
    [leetcode]Word Search @ Python
    [leetcode]Merge Sorted Array @ Python
    [leetcode]Set Matrix Zeroes @ Python
    [leetcode]Restore IP Addresses @ Python
    [leetcode]Interleaving String @ Python
    [leetcode]Distinct Subsequences @ Python
  • 原文地址:https://www.cnblogs.com/songxuexiang/p/11315203.html
Copyright © 2020-2023  润新知