![]() _contains_(), _dir_(), _iter_() and other methods thatĪllow one to do things with an Enum class that fail on a typicalĬlass, such as list(Color) or some_enum_var in Color. The EnumType metaclass is responsible for providing the How are Enums and Flags different? ¶Įnums have a custom metaclass that affects many aspects of both derived EnumĬlasses and their instances (members). The default for Flag is STRICT, the default for IntFlag is EJECT,Īnd the default for _convert_ is KEEP (see ssl.Options for anĮxample of when KEEP is needed). STRICT –> raises an exception when presented with invalid valuesĮJECT –> lose Flag status and become a normal int with the given valueĮxtra bits do show up in repr() and str() _generate_next_value_ – used by the Functional API and byĪuto to get an appropriate value for an enum member may be (class attribute, removed during class creation) _order_ – used in Python 2/3 code to ensure member order is consistent That will not be transformed into members, and will be removed from the final _ignore_ – a list of names, either as a list or a str, ![]() _missing_ – a lookup function used when a value is not found may be _value_ – value of the member can be set / modified in _new_ ![]() OnceĪll the members are created it is no longer used. _new_(), if specified, must create and return the enum members it isĪlso a very good idea to set the member’s _value_ appropriately. _members_ is a read-only ordered mapping of member_name: member > print ( Coordinate ) Coordinate.PY > print ( Coordinate ( 3 )) Coordinate.VY Finer Points ¶ Supported _dunder_ names ¶ def _new_ ( cls, value, label, unit ). Coordinate with binary codes that can be indexed by the int code. %i or %h for IntEnum) treat the enum member as its mixed-in type.Īnd format() will use the enum’s _str_() method. _str_() and _repr_() respectively other codes (such as %-style formatting: %s and %r call the Enum class’s Same as the enum member itself, although it is equivalent and will compareĪ data type is a mixin that defines _new_(). When another data type is mixed in, the value attribute is not the This restriction does not apply to mix-ins which onlyĪdd methods and don’t specify another type. While Enum can have members of any type, once you mix in anĪdditional type, all the members must have values of that type, e.g. Range are not subclassable and will throw an error during Enum When subclassing Enum, mix-in types must appear beforeĮnum itself in the sequence of bases, as in the IntEnum This demonstrates how similar derived enumerations can be defined for exampleĪ FloatEnum that mixes in float instead of int. Boolean value of Enum classes and members.Creating members that are mixed with other data types.Allowed members and attributes of enumerations.Programmatic access to enumeration members and their attributes.
0 Comments
Leave a Reply. |