Skip to content

[Python] how to get enum name in python? #5083

@lixin-wei

Description

@lixin-wei

in java, FlatBuffers will generate name() method for enums, which is very helpful.

  public static final String[] names = { "SUCCESS", "TIME_OUT", "UNKNOWN_ERROR", };
 
  public static String name(int e) { return names[e]; }

so does it in c++

inline const char **EnumNamesResultCode() {

  static const char *names[] = { "SUCCESS", "TIME_OUT", "UNKNOWN_ERROR", nullptr };

  return names;

}

inline const char *EnumNameResultCode(ResultCode e) { return EnumNamesResultCode()[static_cast<int>(e)]; }

but in python, there is no such method. it just generate a pure class with some constants.

class ResultCode(object):

    SUCCESS = 0

    TIME_OUT = 1

    UNKNOWN_ERROR = 2

how to generate name() method in python?

in other words, how can I get enum names in python?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions