Why is __getattr__ not called for indexing operations?

前端 未结 1 1936
故里飘歌
故里飘歌 2021-01-17 01:56

My question:

It seems that __getattr__ is not called for indexing operations, ie I can\'t use __getattr__ on a class

1条回答
  •  有刺的猬
    2021-01-17 02:28

    Python bypasses __getattr__, __getattribute__, and the instance dict when looking up "special" methods for implementing language mechanics. (For the most part, special methods are ones with two underscores on each side of the name.) If you were expecting i[0] to invoke i.__getitem__(0), which would in turn invoke i.__getattr__('__getitem__')(0), that's why that didn't happen.

    0 讨论(0)
提交回复
热议问题