Other Attribute Management Tools

For future reference, also note that there are other ways to manage attribute access in Python:

 

广告:个人专属 VPN,独立 IP,无限流量,多机房切换,还可以屏蔽广告和恶意软件,每月最低仅 5 美元

 
  • The __getattribute__ method intercepts all attribute fetches, not just those that are undefined, but when using it you must be more cautious than with __getattr__ to avoid loops.
  • The property built-in function allows us to associate methods with fetch and set operations on a specific class attribute.
  • Descriptors provide a protocol for associating __get__ and __set__ methods of a class with accesses to a specific class attribute.

Because these are somewhat advanced tools not of interest to every Python programmer, we’ll defer a look at properties until Chapter 31 and detailed coverage of all the attribute management techniques until Chapter 37.