parameters: (message_descriptor, cls)
docstring: Adds an __init__ method to cls.
docstring_summary: Adds an __init__ method to cls.
docstring_tokens: Adds an __init__ method to cls .
function: def _AddInitMethod(message_descriptor, cls):
    """Adds an __init__ method to cls."""
  
    # Create and attach message field properties to the message class.
    # This can be done just once per message class, since property setters and
    # getters are passed the message instance.
    # This makes message instantiation extremely fast, and at the same time it
    # doesn't require the creation of property objects for each message instance,
    # which saves a lot of memory.
    for field in message_descriptor.fields:
      field_cdescriptor = cls.__descriptors[field.name]
      if field.label == _LABEL_REPEATED:
        if field.cpp_type == _CPPTYPE_MESSAGE:
          value = RepeatedCompositeProperty(field_cdescriptor, field.message_type)
        else:
          value = RepeatedScalarProperty(field_cdescriptor)
      elif field.cpp_type == _CPPTYPE_MESSAGE:
        value = CompositeProperty(field_cdescriptor, field.message_type)
      else:
        value = ScalarProperty(field_cdescriptor)
      setattr(cls, field.name, value)
  
      # Attach a constant with the field number.
      constant_name = field.name.upper() + '_FIELD_NUMBER'
      setattr(cls, constant_name, field.number)
  
    def Init(self, **kwargs):
      """Message constructor."""
      cmessage = kwargs.pop('__cmessage', None)
      if cmessage is None:
        self._cmsg = NewCMessage(message_descriptor.full_name)
      else:
        self._cmsg = cmessage
  
      # Keep a reference to the owner, as the owner keeps a reference to the
      # underlying protocol buffer message.
      owner = kwargs.pop('__owner', None)
      if owner is not None:
        self._owner = owner
  
      self.Extensions = ExtensionDict(self)
      self._composite_fields = {}
  
      for field_name, field_value in kwargs.iteritems():
        field_cdescriptor = self.__descriptors.get(field_name, None)
        if field_cdescriptor is None:
          raise ValueError('Protocol message has no "%s" field.' % field_name)
        if field_cdescriptor.label == _LABEL_REPEATED:
          if field_cdescriptor.cpp_type == _CPPTYPE_MESSAGE:
            for val in field_value:
              getattr(self, field_name).add().MergeFrom(val)
          else:
            getattr(self, field_name).extend(field_value)
        elif field_cdescriptor.cpp_type == _CPPTYPE_MESSAGE:
          getattr(self, field_name).MergeFrom(field_value)
        else:
          setattr(self, field_name, field_value)
  
    Init.__module__ = None
    Init.__doc__ = None
    cls.__init__ = Init
function_tokens: def _AddInitMethod ( message_descriptor , cls ) : # Create and attach message field properties to the message class. # This can be done just once per message class, since property setters and # getters are passed the message instance. # This makes message instantiation extremely fast, and at the same time it # doesn't require the creation of property objects for each message instance, # which saves a lot of memory. for field in message_descriptor . fields : field_cdescriptor = cls . __descriptors [ field . name ] if field . label == _LABEL_REPEATED : if field . cpp_type == _CPPTYPE_MESSAGE : value = RepeatedCompositeProperty ( field_cdescriptor , field . message_type ) else : value = RepeatedScalarProperty ( field_cdescriptor ) elif field . cpp_type == _CPPTYPE_MESSAGE : value = CompositeProperty ( field_cdescriptor , field . message_type ) else : value = ScalarProperty ( field_cdescriptor ) setattr ( cls , field . name , value ) # Attach a constant with the field number. constant_name = field . name . upper ( ) + '_FIELD_NUMBER' setattr ( cls , constant_name , field . number ) def Init ( self , * * kwargs ) : """Message constructor.""" cmessage = kwargs . pop ( '__cmessage' , None ) if cmessage is None : self . _cmsg = NewCMessage ( message_descriptor . full_name ) else : self . _cmsg = cmessage # Keep a reference to the owner, as the owner keeps a reference to the # underlying protocol buffer message. owner = kwargs . pop ( '__owner' , None ) if owner is not None : self . _owner = owner self . Extensions = ExtensionDict ( self ) self . _composite_fields = { } for field_name , field_value in kwargs . iteritems ( ) : field_cdescriptor = self . __descriptors . get ( field_name , None ) if field_cdescriptor is None : raise ValueError ( 'Protocol message has no "%s" field.' % field_name ) if field_cdescriptor . label == _LABEL_REPEATED : if field_cdescriptor . cpp_type == _CPPTYPE_MESSAGE : for val in field_value : getattr ( self , field_name ) . add ( ) . MergeFrom ( val ) else : getattr ( self , field_name ) . extend ( field_value ) elif field_cdescriptor . cpp_type == _CPPTYPE_MESSAGE : getattr ( self , field_name ) . MergeFrom ( field_value ) else : setattr ( self , field_name , field_value ) Init . __module__ = None Init . __doc__ = None cls . __init__ = Init