Skip to content

Commit 1deb47a

Browse files
author
remimd
committed
refactoring: ⚡️ Use nonlocal for factory in lazy and alazy functions
1 parent f5210f2 commit 1deb47a

2 files changed

Lines changed: 78 additions & 68 deletions

File tree

injection/_core/common/lazy.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
def lazy[T](factory: Callable[..., T]) -> Callable[[], T]:
88
def cache() -> Iterator[T]:
9+
nonlocal factory
910
value = factory()
11+
del factory
12+
1013
while True:
1114
yield value
1215

@@ -15,7 +18,10 @@ def cache() -> Iterator[T]:
1518

1619
def alazy[T](factory: Callable[..., Awaitable[T]]) -> Callable[[], Awaitable[T]]:
1720
async def cache() -> AsyncIterator[T]:
21+
nonlocal factory
1822
value = await factory()
23+
del factory
24+
1925
while True:
2026
yield value
2127

0 commit comments

Comments
 (0)