diff --git a/book/pythontricks/Chapter.ipynb b/book/pythontricks/Chapter.ipynb index 3b1ef79..82445a1 100644 --- a/book/pythontricks/Chapter.ipynb +++ b/book/pythontricks/Chapter.ipynb @@ -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": {