Skip to content

Instantly share code, notes, and snippets.

@jorenham
Created November 4, 2021 16:43
Show Gist options
  • Save jorenham/a30e679573c41623eec161daec7b7972 to your computer and use it in GitHub Desktop.
Save jorenham/a30e679573c41623eec161daec7b7972 to your computer and use it in GitHub Desktop.
async version of __init__
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