Initializing Python static class attributes in the constructor call

93 views Asked by At

In some Google documentation there is the following code (abbreviated for clarity). The class Note is defined and then instantiated with a parameter in the constructor call. I wasn't aware that attributes could be initialized this way. Is this a native Python feature or some magic that is happening in the 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!')
1

There are 1 answers

5
kindall On BEST ANSWER

It doesn't work the way you're thinking. The class Note inherits from Message. Message (or some other class it inherits from) has code in its __init__() method to do this. There's no magic.