Created
November 4, 2021 16:43
-
-
Save jorenham/a30e679573c41623eec161daec7b7972 to your computer and use it in GitHub Desktop.
async version of __init__
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
class AsyncInit: | |
def __await__(self): | |
async def _(): | |
await self.__ainit__() | |
return self | |
return _().__await__() | |
async def __ainit__(self) -> None: | |
... | |
class AsyncSpam(AsyncInit): | |
def __init__(self, value): | |
self.value = value | |
self.initialized = False | |
async def __ainit__(self): | |
await asyncio.sleep(1) | |
self.initialized = True | |
async def amain(): | |
spam = await AsyncSpam('ham') | |
assert spam.initialized | |
print(spam.value) | |
asyncio.run(amain()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment