Skip to content

Commit 26008eb

Browse files
committed
add example for LazySharedInstance
1 parent aa49385 commit 26008eb

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

beets/util/__init__.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,32 @@ class LazySharedInstance(Generic[T]):
10461046
"""A descriptor that provides access to a lazily-created shared instance of
10471047
the containing class, while calling the class constructor to construct a
10481048
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+
```
10491075
"""
10501076

10511077
_instance: T | None = None

0 commit comments

Comments
 (0)