Skip to content

Commit

Permalink
Concept Entry - Python: Enum (#5736)
Browse files Browse the repository at this point in the history
* title table added

* drafted enum entry, ready for pr

* check fail fix attempt 1

* check fail fix attempt 2

* implemented comments

* Update enum.md

minor fixes

* Update enum.md

* Update content/python/concepts/enum/enum.md

* Update content/python/concepts/enum/enum.md

* Update content/python/concepts/enum/enum.md

---------
  • Loading branch information
danitellini authored Dec 19, 2024
1 parent b6cebe1 commit 915e13d
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions content/python/concepts/enum/enum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
---
Title: 'enum'
Description: 'A class that defines a set of named values, providing a structured way to represent constant values in a readable manner.'
Subjects:
- 'Code Foundations'
- 'Computer Science'
Tags:
- 'Data Types'
- 'Enum'
- 'Python'
- 'Variables'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
---

**`Enum`** (short for _enumeration_) is a class in Python used to define a set of named, immutable constants. Enumerations improve code readability and maintainability by replacing magic numbers or strings with meaningful names. Enums are part of Python's built-in `enum` module, introduced in Python 3.4.

> **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

```pseudo
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 enumerations. It also includes:

- `IntEnum`: Ensures that the values of the enuemration are integers.
- `Flag`: Allows combining constants with bitwise operations.
- `Auto`: Automatically assigns values to the enumeration members.

Enums also provide methods like:

- `.name`: Returns the name of the enum member (as a string).
- `.value`: Returns the value assigned to the enum member.

## Example

This example demonstrates how to create an enum for days of the week with integer values:

```py
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 example results in the following output:

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

## Codebyte

This example demonstrates how enums can represent traffic light states and associate actions with each state:

```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))
```

0 comments on commit 915e13d

Please sign in to comment.