Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23276

Fluent pattern with async methods

$
0
0

This class has async and sync methods (i.e. isHuman):

class Character:  def isHuman(self) -> Self:    if self.human:      return self    raise Exception(f'{self.name} is not human')  async def hasJob(self) -> Self:    await asyncio.sleep(1)    return self  async def isKnight(self) -> Self:    await asyncio.sleep(1)    return self

If all methods were sync, I'd have done:

# Fluent patternjhon = (  Character(...)  .isHuman()  .hasJob()  .isKnight())

I know I could do something like:

jhon = Character(...).isHuman()await jhon.hasJob()await jhon.isKnight()

But I'm looking for something like this:

jhon = await (  Character(...)  .isHuman()  .hasJob()  .isKnight())

Viewing all articles
Browse latest Browse all 23276

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>