Last active
April 4, 2024 14:45
-
-
Save ivangeorgiev/d4bd94bc69f14fe35324f4855a4db9f1 to your computer and use it in GitHub Desktop.
Python Singleton Class with Decorators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Python Singleton Class with Decorators", | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyPUKNA9PvBgIaSGqHTcPQTn", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "fUYU5-0ZdtiH", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## Define the decorator" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "ItFzu5Y6dfUN", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"from functools import wraps\n", | |
"\n", | |
"def singleton(orig_cls):\n", | |
" orig_new = orig_cls.__new__\n", | |
" instance = None\n", | |
"\n", | |
" @wraps(orig_cls.__new__)\n", | |
" def __new__(cls, *args, **kwargs):\n", | |
" nonlocal instance\n", | |
" if instance is None:\n", | |
" instance = orig_new(cls, *args, **kwargs)\n", | |
" return instance\n", | |
" orig_cls.__new__ = __new__\n", | |
" return orig_cls" | |
], | |
"execution_count": 1, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "MY1TVedgdy0O", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## Define a decorated class" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "ggiY3_1sdxwr", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"@singleton\n", | |
"class Logger:\n", | |
" def log(msg):\n", | |
" print(msg)" | |
], | |
"execution_count": 3, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "2uzA8Pokd8gn", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## Assert that only a single instance is created\n", | |
"\n", | |
"Following assert statement makes sure two variables are referencing the same object.\n", | |
"\n", | |
"```python\n", | |
"assert var1 is var2\n", | |
"```\n", | |
"\n", | |
"If `var1` is referring to a different object than `var2`, than the `assert` operator will raise `AssertionError`.\n", | |
"\n", | |
"In our case assertion is successful. No error is raised." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "pZvEqaZEd5Wa", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"logger1 = Logger()\n", | |
"logger2 = Logger()\n", | |
"assert logger1 is logger2" | |
], | |
"execution_count": 4, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "-xW9DdiJeBob", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"" | |
], | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment