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

时间:2016-01-08 06:18:42

标签: python constructor static-members protorpc

在某些Google documentation中,有以下代码(为清晰起见,缩写)。定义类Note,然后在构造函数调用中使用参数进行实例化。我不知道可以通过这种方式初始化属性。这是一个本机Python特性还是Message超类中发生的一些魔法?

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!')

1 个答案:

答案 0 :(得分:1)

它不像你想的那样有效。课程Note继承自MessageMessage(或其继承的其他类)在其__init__()方法中使用代码来执行此操作。没有魔力。