Skip to content

Commit

Permalink
Add enum
Browse files Browse the repository at this point in the history
  • Loading branch information
baniasbaabe committed Feb 11, 2024
1 parent 543c17b commit 95699ba
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions book/pythontricks/Chapter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,51 @@
" def push(self, item: T) -> None:\n",
" self.items.append(item)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Enumerations in Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To make your Python Code cleaner, use `Enums`.\n",
"\n",
"`Enums` (or enumerations) are a way to represent a set of named values as symbolic names.\n",
"\n",
"It provides a way to work with meaningful names instead of raw integers or weird string constants.\n",
"\n",
"Python supports enums with its standard library."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Option 1\n",
"from enum import Enum, auto\n",
"\n",
"class Color(Enum):\n",
" RED = 1\n",
" GREEN = 2\n",
" BLUE = 3\n",
" \n",
"print(Color.RED.value) # Output: 1\n",
"\n",
"# Option 2: auto() assigns unique values starting by 1\n",
"class Status(Enum):\n",
" PENDING = auto()\n",
" APPROVED = auto()\n",
" REJECTED = auto()\n",
" \n",
"print(Status.PENDING.value) # Output: 2"
]
}
],
"metadata": {
Expand Down

0 comments on commit 95699ba

Please sign in to comment.