File tree Expand file tree Collapse file tree 1 file changed +39
-1
lines changed
Expand file tree Collapse file tree 1 file changed +39
-1
lines changed Original file line number Diff line number Diff line change 11#!/usr/bin/env python3
22# -*- coding: utf-8 -*-
33
4+ from typing import Type , Any , Dict
5+
6+
47class SingletonMeta (type ):
5- _instance = {}
8+ """
9+ A metaclass for implementing the Singleton design pattern.
10+
11+ This metaclass ensures that a class has only one instance and provides a global point of
12+ access to that instance.
13+
14+ Usage
15+ -----
16+ ```
17+ class MyClass(metaclass=SingletonMeta):
18+ pass
19+ ```
20+
21+ Now, `MyClass` will have only one instance, and subsequent calls to `MyClass()` will return
22+ the existing instance.
23+ """
24+ _instance : Dict [Type , Any ] = {}
625
726 def __call__ (cls , * args , ** kwargs ):
27+ """
28+ Call method for creating an instance of the class.
29+
30+ Parameter
31+ ---------
32+ cls: Type
33+ The class to create an instance of.
34+
35+ *args
36+ Positional arguments to be passed to the class constructor.
37+
38+ **kwargs
39+ Keyword arguments to be passed to the class constructor.
40+
41+ Returns
42+ -------
43+ Any
44+ The instance of the class.
45+ """
846 if cls not in cls ._instance :
947 cls ._instance [cls ] = super (SingletonMeta , cls ).__call__ (* args , ** kwargs )
1048 return cls ._instance [cls ]
You can’t perform that action at this time.
0 commit comments