Dynamic Attributes and Properties

The crucial importance of properties is that their existence makes it perfectly safe and indeed advisable for you to expose public data attributes as part of your class's public interface.

Data attributes and methods are collectively known as attibutes in Python: a method is just a attribute that is callable.  Besides data attributes and methods, we can also create properties, which can be used to replace a public data attribute with accessor methods (i.g., getter/setter), without changing the class interface. 

Python provides a rich API for controlling attribute access and implementing dynamic attributes.

Coding dynamic attributes is the kind of metaprogramming that framework authors always do. But in Python, the basic techniques are so straightforward that anyone can put them to work.

@staticmethod 官方链接(类和实例都可以直接调用类方法): https://docs.python.org/3.6/library/functions.html?highlight=classmethod#classmethod

end ...

原文地址:https://www.cnblogs.com/neozheng/p/12585449.html