How can I override class attribute access in python?

前端 未结 2 715
不思量自难忘°
不思量自难忘° 2020-12-18 23:26

How can I override class attribute access in python?

P.S. Is there a way to leave regular access to class attributes alone but calling a more specific exception on m

2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-18 23:49

    agf's answer is correct, and in fact the answer I'm about to give is based on it and owes it some credit.

    Just to give more detail on the class-attribute side, which is the part that's actually under question here (as opposed to the difference between __getattr__ and __getattribute__), I'd like to add a reference to an answer I wrote to a different similar question, which goes into more detail on the difference between class and instance attributes, how this is implemented, and why you need to use a metaclass to influence class attribute lookup.

提交回复
热议问题