9
9
# https://github.com/scottzach1/python-injector-framework
10
10
11
11
import abc
12
+ import functools
12
13
from typing import Callable
13
14
14
15
@@ -46,17 +47,15 @@ class Singleton[T](Provider):
46
47
Provide a singleton instance.
47
48
"""
48
49
49
- __slots__ = ("_func" , "_args" , "_kwargs " , "_result" , "_depends" )
50
+ __slots__ = ("_func" , "_func " , "_result" , "_depends" )
50
51
51
52
def __init__ (self , func : Callable [[...], T ], * args , ** kwargs ):
52
- self ._func = func
53
- self ._args = args
54
- self ._kwargs = kwargs
53
+ self ._func = functools .partial (func , * args , ** kwargs )
55
54
self ._result = UNSET
56
55
57
56
def __call__ (self ) -> T :
58
57
if self ._result is UNSET :
59
- self ._result = self ._func (* self . _args , ** self . _kwargs )
58
+ self ._result = self ._func ()
60
59
return self ._result
61
60
62
61
@@ -65,12 +64,10 @@ class Factory[T](Provider):
65
64
Generate a new instance every call.
66
65
"""
67
66
68
- __slots__ = ("_func" , "_args" , "_kwargs" , " _depends" )
67
+ __slots__ = ("_func" , "_depends" )
69
68
70
69
def __init__ (self , func : Callable [[...], T ], * args , ** kwargs ):
71
- self ._func = func
72
- self ._args = args
73
- self ._kwargs = kwargs
70
+ self ._func = functools .partial (func , * args , ** kwargs )
74
71
75
72
def __call__ (self ) -> T :
76
- return self ._func (* self . _args , ** self . _kwargs )
73
+ return self ._func ()
0 commit comments