Skip to content

Commit 3d06904

Browse files
committed
💡 style: add some comment
1 parent 72d60f6 commit 3d06904

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

code_counter/tools/singleton.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,48 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4+
from typing import Type, Any, Dict
5+
6+
47
class 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]

0 commit comments

Comments
 (0)