Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concept Entry - Python: Enum #5736

Merged
merged 16 commits into from
Dec 19, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions content/python/concepts/enum/enum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
---
Title: '`enum`'
Description: 'Represents a set of unique, immutable constants in Python.'
danitellini marked this conversation as resolved.
Show resolved Hide resolved
Subjects:
- 'Code Foundations'
- 'Computer Science'
Tags:
- 'Data Types'
- 'Enum'
- 'Python'
- 'Variables'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
---

**`Enum`** (short for _enumeration_) is a data type in Python used to define a set of named, immutable constants.
danitellini marked this conversation as resolved.
Show resolved Hide resolved

Enumerations make code more readable and maintainable by replacing **magic numbers** or strings with meaningful names.

Enums are part of Python's built in `enum` module, introduced in Python 3.4.
danitellini marked this conversation as resolved.
Show resolved Hide resolved

> **Note:** Magic numbers are unclear, hardcoded values in code. For example, `80` in a speed-checking program might be confusing. Replacing it with an enum constant, like `SpeedLimit.HIGHWAY`, makes the code easier to read and maintain.

## Syntax

```python
from enum import Enum

class EnumName(Enum):
MEMBER1 = value1
MEMBER2 = value2
```

- `EnumName`: The name of the enum class.
- `MEMBER1`, `MEMBER2`: Names of the constants.
- `value1`, `value2`: Values assigned to the constants (e.g. numbers or strings).

## `enum` Module

The `enum` module provides the `Enum` class for creating enums. It also includes:
danitellini marked this conversation as resolved.
Show resolved Hide resolved

- `IntEnum`: Ensures values are integers.
- `Flag`: Allows combining constants with bitwise operations.
- `Auto`: Automatically assigns values.
danitellini marked this conversation as resolved.
Show resolved Hide resolved

Enums provide methods like `.name` (the constant name) and `.value` (the assigned value).
danitellini marked this conversation as resolved.
Show resolved Hide resolved

## Example

This example shows how to create an enum for days of the week:
danitellini marked this conversation as resolved.
Show resolved Hide resolved

```python
danitellini marked this conversation as resolved.
Show resolved Hide resolved
from enum import Enum

class Weekday(Enum):
MONDAY = 1
TUESDAY = 2
WEDNESDAY = 3

# Accessing members
print(Weekday.MONDAY)
print(Weekday.MONDAY.name)
print(Weekday.MONDAY.value)

# Iterating through members
for day in Weekday:
print(day)
```

This would output with the following:
Radhika-okhade marked this conversation as resolved.
Show resolved Hide resolved

```shell
Weekday.MONDAY
MONDAY
1
Weekday.MONDAY
Weekday.TUESDAY
Weekday.WEDNESDAY
```

## Codebyte

Here is how enums can represent traffic light states:
danitellini marked this conversation as resolved.
Show resolved Hide resolved

```codebyte/python
from enum import Enum

class TrafficLight(Enum):
RED = 'Stop'
YELLOW = 'Caution'
GREEN = 'Go'

def traffic_action(light):
if light == TrafficLight.RED:
return "Stop your car."
elif light == TrafficLight.YELLOW:
return "Prepare to stop."
elif light == TrafficLight.GREEN:
return "You can go."

# Example usage
current_light = TrafficLight.RED
print(traffic_action(current_light))
```
Loading