-
Notifications
You must be signed in to change notification settings - Fork 0
/
metaclass_call_prints.py
59 lines (44 loc) · 1.42 KB
/
metaclass_call_prints.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from functools import wraps
def print_deco(func):
if type(func) == type or func.__qualname__ == "Logger":
print(f"\nClass {func.__qualname__} registered.\n")
@wraps(func)
def wrapper(*args, **kwargs):
print(f"{func.__qualname__}() called with args:\n\t{func.__qualname__.replace(f'.{func.__name__}', '')}, {str(args[1:])}")
result = func(*args, **kwargs)
print(f"\tResult of {func.__qualname__}: {result}")
return result
return wrapper
@print_deco
class Singleton(type):
_instances = {}
@print_deco
def __init__(cls, *args, **kwargs):
super().__init__(*args, **kwargs)
@print_deco
def __new__(cls, *args, **kwargs):
cls.__new__.called = True
return super().__new__(cls, *args, **kwargs)
@print_deco
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super().__call__(*args, **kwargs)
return cls._instances[cls]
@print_deco
def mro(self, *args, **kwargs):
return super().mro(*args, **kwargs)
@print_deco
def __prepare__(self, *args, **kwargs):
return {}
@print_deco
class Logger(metaclass=Singleton):
@print_deco
def __init__(self):
super().__init__()
@print_deco
def __new__(cls, *args, **kwargs):
return super().__new__(cls)
print("\nFirst instance:\n")
Logger()
print("\nSecond instance:\n")
Logger()