File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -1046,6 +1046,32 @@ class LazySharedInstance(Generic[T]):
1046
1046
"""A descriptor that provides access to a lazily-created shared instance of
1047
1047
the containing class, while calling the class constructor to construct a
1048
1048
new object works as usual.
1049
+
1050
+ ```
1051
+ ID: int = 0
1052
+
1053
+ class Foo:
1054
+ def __init__():
1055
+ global ID
1056
+
1057
+ self.id = ID
1058
+ ID += 1
1059
+
1060
+ def func(self):
1061
+ print(self.id)
1062
+
1063
+ shared: LazySharedInstance[Foo] = LazySharedInstance()
1064
+
1065
+ a0 = Foo()
1066
+ a1 = Foo.shared
1067
+ a2 = Foo()
1068
+ a3 = Foo.shared
1069
+
1070
+ a0.func() # 0
1071
+ a1.func() # 1
1072
+ a2.func() # 2
1073
+ a3.func() # 1
1074
+ ```
1049
1075
"""
1050
1076
1051
1077
_instance : T | None = None
You can’t perform that action at this time.
0 commit comments