mirror of
https://github.com/beetbox/beets.git
synced 2025-12-06 08:39:17 +01:00
add example for LazySharedInstance
This commit is contained in:
parent
aa49385d27
commit
26008eb922
1 changed files with 26 additions and 0 deletions
|
|
@ -1046,6 +1046,32 @@ class LazySharedInstance(Generic[T]):
|
||||||
"""A descriptor that provides access to a lazily-created shared instance of
|
"""A descriptor that provides access to a lazily-created shared instance of
|
||||||
the containing class, while calling the class constructor to construct a
|
the containing class, while calling the class constructor to construct a
|
||||||
new object works as usual.
|
new object works as usual.
|
||||||
|
|
||||||
|
```
|
||||||
|
ID: int = 0
|
||||||
|
|
||||||
|
class Foo:
|
||||||
|
def __init__():
|
||||||
|
global ID
|
||||||
|
|
||||||
|
self.id = ID
|
||||||
|
ID += 1
|
||||||
|
|
||||||
|
def func(self):
|
||||||
|
print(self.id)
|
||||||
|
|
||||||
|
shared: LazySharedInstance[Foo] = LazySharedInstance()
|
||||||
|
|
||||||
|
a0 = Foo()
|
||||||
|
a1 = Foo.shared
|
||||||
|
a2 = Foo()
|
||||||
|
a3 = Foo.shared
|
||||||
|
|
||||||
|
a0.func() # 0
|
||||||
|
a1.func() # 1
|
||||||
|
a2.func() # 2
|
||||||
|
a3.func() # 1
|
||||||
|
```
|
||||||
"""
|
"""
|
||||||
|
|
||||||
_instance: T | None = None
|
_instance: T | None = None
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue