在构造函数调用中初始化 Python 静态 class 属性

Initializing Python static class attributes in the constructor call

在某些 Google documentation 中有以下代码(为清楚起见缩写)。 class Note 被定义,然后在构造函数调用中用参数实例化。我不知道可以用这种方式初始化属性。这是本机 Python 功能还是 Message superclass 中发生的一些魔法?

from protorpc import messages

class Note(messages.Message):

    text = messages.StringField(1, required=True)

# Import the standard time Python library to handle the timestamp.

note_instance = Note(text = u'Hello guestbook!')

它并不像你想的那样工作。 class Note 继承自 MessageMessage(或它继承自的其他 class)在其 __init__() 方法中有执行此操作的代码。没有魔法。