Yes, Python enums can hold multiple values per member, but the standard library enum module does not support this directly out of the box. You can achieve it by subclassing Enum and overriding the __new__ method to store extra attributes, or by using a helper like MultiValueEnum from the aenum library. The most common approach is to attach additional data to each enum member while keeping a single canonical value for comparisons.
What an Enum Actually Is in Python
An Enum is a set of symbolic names bound to unique, constant values. In the standard library, each member is an instance of the enum class, and its .value attribute holds the single value you assigned. For example:
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
print(Color.RED.value) # 1
This design ensures that members are hashable, comparable by identity, and can be iterated in definition order. The limitation is that the value is a single object. If you try to assign a tuple like RED = (1, 'red', '#FF0000'), the tuple becomes the value, and you lose the ability to access the individual components as separate attributes without unpacking.
Why You Might Need Multiple Values
Real-world data rarely fits a single scalar. You might want an enum member to carry a numeric code, a human-readable label, and a color hex string. For example, a status enum could have ACTIVE = (1, 'Active', 'green'). Storing all three in a tuple works, but it is awkward because you must remember the order and index positions. A better design is to give each member named attributes like .code, .label, and .color. This makes the code self-documenting and less error-prone.
How to Add Multiple Values with __new__
The idiomatic way to attach extra data to an enum member is to override __new__. When you define a member, Python calls __new__ with the member name and the values you provide. You can intercept that call, store the extra values as attributes on the new instance, and then set the first value as the canonical _value_. Here is a complete example:
from enum import Enum
class Status(Enum):
def __new__(cls, code, label, color):
obj = object.__new__(cls)
obj._value_ = code # canonical value for comparisons
obj.code = code
obj.label = label
obj.color = color
return obj
ACTIVE = (1, 'Active', 'green')
INACTIVE = (2, 'Inactive', 'gray')
ERROR = (3, 'Error', 'red')
print(Status.ACTIVE.value) # 1
print(Status.ACTIVE.label) # 'Active'
print(Status.ERROR.color) # 'red'
Note that _value_ must be set to the first element if you want Status.ACTIVE == 1 to work. If you prefer the tuple itself as the value, you can set obj._value_ = (code, label, color), but then equality checks against a plain integer will fail.
Using the aenum Library for Simpler Syntax
If you do not want to write the __new__ boilerplate every time, the third-party aenum package provides a MultiValueEnum class. Install it with pip install aenum, then:
from aenum import MultiValueEnum
class Status(MultiValueEnum):
ACTIVE = (1, 'Active', 'green')
INACTIVE = (2, 'Inactive', 'gray')
ERROR = (3, 'Error', 'red')
print(Status.ACTIVE.value) # 1
print(Status.ACTIVE.label) # 'Active'
This library also supports aliases and other advanced features. However, it is an external dependency, so weigh that against the standard library approach if you want zero dependencies.
Common Pitfalls and Misconceptions
One frequent mistake is assuming that assigning a tuple automatically gives you named attributes. It does not; you get a tuple as the value. Another pitfall is forgetting that __new__ must return an instance of the enum class, not just any object. Also, if you override __new__, you should not also override __init__ unless you are careful, because __init__ will receive the same arguments after __new__ and might overwrite your attributes. A common misconception is that you can have multiple values for lookup, meaning Status(1) and Status('Active') both return the same member. That requires a different technique: overriding _missing_ to handle alternative lookup keys. The standard library does not support this by default, but you can implement it as shown in the official docs.
How to Choose Between Tuple Values and Named Attributes
If you only need to store a fixed set of related constants and rarely access them by name, a simple tuple value may be sufficient. For example, a coordinate pair POINT = (10, 20) is fine. But if you have more than two or three values, or if the meaning of each position is not obvious, named attributes are much clearer. Also, if you need to add methods to the enum that use the extra data, named attributes make the code more readable. Rule of thumb: if you find yourself writing member.value[1] more than once, switch to named attributes.
Alternatives to Enums with Multiple Values
If your data is complex, consider using a dataclass instead of an enum. For example, you could define a class with class-level constants of that dataclass type. This gives you full flexibility with fields, defaults, and methods. Another alternative is a dictionary mapping a key to a tuple or a small class. For simple cases, a namedtuple inside an enum can work, but it still requires the __new__ override to unpack it. The choice depends on whether you need the enum-specific features like iteration, membership testing, and automatic __str__ and __repr__.
Frequently Asked Questions
Can I have two values that both map to the same enum member? Yes, that is called an alias. In the standard library, you can assign two names to the same value, and both names refer to the same member. For example, RED = 1 and CRIMSON = 1 will make Color.RED is Color.CRIMSON true. This is different from having multiple values per member; it is multiple names for one value.
How do I look up an enum member by one of its extra values? You can write a classmethod that iterates over members and checks the attribute. For instance, @classmethod def from_label(cls, label): return next(m for m in cls if m.label == label). This works but is O(n). For frequent lookups, build a dictionary once.
Does enum.auto() work with multiple values? No, auto() only generates a single integer value. If you need multiple values, you must provide them explicitly. You could combine auto() with a tuple, but then the tuple is the value, and you lose the auto-incrementing integer as a separate attribute.
Is it safe to use mutable objects as enum values? No, and the standard library will raise an error if you try to use a list or dict as a value. Enum values must be hashable and immutable. If you need mutable data, store it as an attribute that is not the _value_, but be aware that mutating it could break assumptions about enum immutability.
Can I pickle an enum with multiple values? Yes, as long as the extra attributes are picklable. The standard pickle protocol works with enum members, and it will preserve the custom attributes you set in __new__. Just ensure your class is defined at module level.
Related
- Data Entry How to Do: Steps, Tools, and Accuracy Tips
- How to Select All Files in a Folder (Windows & Mac)
- Select All Cells in a Range: Excel & Sheets
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Google Sheets: How to Share With OthersTo share a Google Sheet, open it in a web browser, click Share in the top-right corner, add…
- How to Make Two Columns in Microsoft Word: A GuideTo make two columns in Microsoft Word for Microsoft 365 on Windows, open the Layout tab, choose Columns,…
- Excel String + String: How to Join TextIn Excel, + adds numbers; it does not join ordinary text strings. To combine text, use &: with…
- How to Change the Ribbon View in Outlook on Any DeviceTo change the ribbon view in Outlook, use the ribbon display control at the top of the window…
- How to Enable an Audit Trail in Excel: Track ChangesTo track edits in Excel for Microsoft 365, save the workbook to OneDrive or SharePoint, then select Review…
- Excel Keyboard Shortcut to Insert a Row or LineIn Excel for Microsoft 365 on Windows, select a row and press Ctrl+Shift+= to insert a worksheet row…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.