Difference between __getattr__ vs __getattribute__

前端 未结 8 1054
甜味超标
甜味超标 2020-11-22 12:54

I am trying to understand when to use __getattr__ or __getattribute__. The documentation mentions __getattribute__ applies to new-sty

8条回答
  •  隐瞒了意图╮
    2020-11-22 13:02

    I find that no one mentions this difference:

    __getattribute__ has a default implementation, but __getattr__ does not.

    class A:
        pass
    a = A()
    a.__getattr__ # error
    a.__getattribute__ # return a method-wrapper
    

    This has a clear meaning: since __getattribute__ has a default implementation, while __getattr__ not, clearly python encourages users to implement __getattr__.

提交回复
热议问题