2015-04-15 10 views
5

seguito sono riportati i contenuti del protocollo di Google Buffer (Proto) FileCome impostare un campo ProtoBuf che è un messaggio vuoto in Python?

message First 
{ 
    required uint32 field1 = 1; 

    optional MessageType1 request = 2; 
} 

message MessageType1 
{ 
} 

voglio impostare la richiesta campo MessageType1. Ma ottengo questo come un errore:

AttributeError: Assignment not allowed to composite field "request" in protocol message object. 

Come impostare il valore di questo messaggio vuoto in Python?

risposta

8

Ottenuto nel codice sorgente della classe Messaggio in Proto Buffer.

def SetInParent(self): 
    """Mark this as present in the parent. 

    This normally happens automatically when you assign a field of a 
    sub-message, but sometimes you want to make the sub-message 
    present while keeping it empty. If you find yourself using this, 
    you may want to reconsider your design.""" 

Quindi il modo per impostare un messaggio di tale vuoto è quello di chiamare questa funzione:

first.request.SetInParent() 
+0

provato 'first.request.Clear()' ha lo stesso effetto. –