How to set class attribute with await in __init__

后端 未结 5 2065
醉话见心
醉话见心 2020-11-28 19:04

How can I define a class with await in the constructor or class body?

For example what I want:

im         


        
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 19:49

    [Almost] canonical answer by @ojii

    @dataclass
    class Foo:
        settings: Settings
        pool: Pool
    
        @classmethod
        async def create(cls, settings: Settings, dsn):
            return cls(settings, await create_pool(dsn))
    

提交回复
热议问题