python序列化proto时对repeated修饰数据进行赋值(常用类型和其他类型)

一、该列表是常用类型,比如int

message C2GS_GoodsList {
  repeated int32 typeList = 1;
}

对repeated修饰数据进行赋值需要用append

goodsList= C2GS_GoodsList()
goodsList.typeList.append(1)
goodsList.typeList.append(2)
goodsList.typeList.append(3)

 

二、该列表类型是另一个类

message prop {

  required int32 propId=1;
  required int32 propNum=2;

}

message C2GS_B {
  repeated prop propRespVo = 1;
}

对repeated修饰数据进行赋值需要用add()

goodsList = C2GS_B ()

data = goodsList.propRespVo.add()

data.propId=1001

data.propNum=2

 

 三、对required和optional类型赋值

message prop {

  required int32 propId=1;
  required int32 propNum=2;

}

message C2GS_B {
  required prop propRespVo = 1;
}

required和optional类型赋值直接用对象.属性名

goodsList = C2GS_B ()

goodsList.propRespVo.propId=1001

goodsList.propRespVo.propNum=2

转载于:https://www.cnblogs.com/lucio1128/p/10521846.html

你可能感兴趣的:(python序列化proto时对repeated修饰数据进行赋值(常用类型和其他类型))