I have this piece of code:
import enum class Color(enum.Enum): RED = '1' BLUE = '2' GREEN = '3' def get_color_return_something(some_color): pass
How do I properly add type annotations to the some_color
variable in this function, if I suppose that I'll receive an enum attribute from the Color enum (for example: Color.RED
)?
The enum can be of any numeric data type such as byte, sbyte, short, ushort, int, uint, long, or ulong. However, an enum cannot be a string type.
Definition. An Enumeration (or enum ) is a data type that includes a set of named values called elements or members. The enumerator names are usually identifiers that behave as constants in the language.
Enum is a class in python for creating enumerations, which are a set of symbolic names (members) bound to unique, constant values. The members of an enumeration can be compared by these symbolic anmes, and the enumeration itself can be iterated over.
Enums are one of the few features TypeScript has which is not a type-level extension of JavaScript. Enums allow a developer to define a set of named constants. Using enums can make it easier to document intent, or create a set of distinct cases. TypeScript provides both numeric and string-based enums.
Type hinting the Color class should work:
def get_color_return_something(some_color: Color): print(some_color.value)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With