Python类是在运行时动态生成的。在
引用您链接到的文档页面:The important line in each class is metaclass =
reflection.GeneratedProtocolMessageType. While the details of how
Python metaclasses work is beyond the scope of this tutorial, you can
think of them as like a template for creating classes. At load time,
the GeneratedProtocolMessageType metaclass uses the specified
descriptors to create all the Python methods you need to work with
each message type and adds them to the relevant classes. You can then
use the fully-populated classes in your code.
更具体地说就是:Person = _reflection.GeneratedProtocolMessageType('Person', (_message.Message,), dict(...
Person在Python类中创建。在
在Python中,类似于:
^{pr2}$
与定义常规的Person类相同。这就是在引擎盖下面用的东西。在
如果您不太熟悉元类的概念,您可能需要看看SO question或这个blog entry。在
根据上一条评论进行编辑
我不确定我是否正确地理解了你的最后一句话,但是
如果要在foo.py中使用来自addressbook_pb2.py的Person类,
将addressbook_pb2.py与foo.py相邻,并在foo.py中执行以下导入操作:from addressbook_pb2 import Person