Skip to content

Commit 90aa548

Browse files
committed
Update enum.md
1 parent 07c611f commit 90aa548

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

python-stdlib/enum/enum.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,66 @@ The base class for all enumerations.
114114
* Raised when attempting to add new members to an initialized Enum.
115115
* Raised when a class-level lookup (`Status(999)`) fails.
116116
* Raised when an instance-level lookup (`s(999)`) fails.
117+
118+
## Compare with CPython
119+
120+
```python
121+
# Run on MicroPython v1.28.0 on 2026-04-06; Generic ESP32 module with ESP32
122+
# Run on Python 3.12.10
123+
from enum import Enum
124+
125+
# class syntax
126+
class Color(Enum):
127+
RED = 1
128+
GREEN = 2
129+
BLUE = 3
130+
131+
# OR
132+
# functional syntax
133+
# Color = Enum('Color', {'RED': 1, 'GREEN': 2, 'BLUE': 3})
134+
135+
# List enum members
136+
try:
137+
print(list(Color))
138+
# [<Color.RED: 1>, <Color.GREEN: 2>, <Color.BLUE: 3>]
139+
except:
140+
print(Color.list())
141+
# [RED: 1, GREEN: 2, BLUE: 3]
142+
143+
# Accessing enum member by name
144+
print(Color.GREEN, type(Color.GREEN))
145+
# Color.GREEN <enum 'Color'>
146+
# GREEN: 2 <class 'EnumValue'>
147+
148+
# Accessing enum member by name
149+
try:
150+
print(Color['GREEN'])
151+
# Color.GREEN
152+
except:
153+
print(Color('GREEN'))
154+
# GREEN: 2
155+
156+
# Accessing enum member by value
157+
print(Color(2))
158+
# Color.GREEN
159+
160+
# Accessing enum member name
161+
print(Color.GREEN.name, type(Color.GREEN.name))
162+
# GREEN <class 'str'>
163+
164+
# Accessing enum member value
165+
print(Color.GREEN.value, type(Color.GREEN.value))
166+
# 2 <class 'int'>
167+
```
168+
169+
### Output is:
170+
171+
| MicroPython v1.28.0 | Python 3.12.10 |
172+
| :--- | :--- |
173+
| [RED: 1, GREEN: 2, BLUE: 3] | [<Color.RED: 1>, <Color.GREEN: 2>, <Color.BLUE: 3>] |
174+
| GREEN: 2 <class 'EnumValue'> | Color.GREEN <enum 'Color'> |
175+
| GREEN: 2 | Color.GREEN |
176+
| GREEN: 2 | Color.GREEN |
177+
| GREEN <class 'str'> | GREEN <class 'str'> |
178+
| 2 <class 'int'> | 2 <class 'int'> |
179+

0 commit comments

Comments
 (0)