如何访问 protobufs 中的 python 枚举

How to access python enums in protobufs

在我名为 skill.proto 的 protobuf 文件中,我有:

message Cooking {
    enum VegeType {
        CAULIFLOWER = 0;
        CUCUMBER = 1;
    }
    required VegeType type = 1;
}

在另一个文件中(例如:name.py)我想将烹饪类型设置为黄瓜。即:

co = skill_pb2.Cooking()
co.type = skill_pb2.cooking.type.CUCUMBER

所以最后一行不起作用。 如何将 co.type 设置为 CUCUMBER?

注意:我想避免做 co.type = 1

只是一个错字和一些大写。

skill_pb2.Cooking.CUCUMBER

https://developers.google.com/protocol-buffers/docs/pythontutorial


更新:protobuf 中 accessing enums 现在有三种可能的方法:

skill_pb2.Cooking.CUCUMBER
skill_pb2.Cooking.VegeType.CUCUMBER
skill_pb2.Cooking.VegeTypeValue.Value('CUCUMBER')

this issue.

中所述,第二个更近