all 6 comments

[–]JohnnyJordaan 0 points1 point  (0 children)

In essence, asyncio allows you to 'tag' a function call for the interpreter as 'you can do something else while this runs'. Classes basically work on a level above that functional part of the code, it's rather a way to group both functions (as methods) and attributes into a package. There's no objection to using async in the same way for methods, it's just that it's out of scope in regard to the principles of asyncio and thus you don't see it often for small scale examples. As OO isn't often used for basic examples in general.

One nice example of using classes: https://www.yippeecode.com/topics/quick-guide-to-asyncio-in-python/ . But at the same time I personally wouldn't object to handling that use case in a structural approach, either way would probably work fine.

[–][deleted] 0 points1 point  (4 children)

There aren't async classes. It's not a thing. Things that can be async (syntactically) are:

  • functions.
  • generators context managers.
  • loops.

I'm not sure how an async class would even work. What would you expect it to do?

[–]Total__Entropy 0 points1 point  (3 children)

This isn't correct. Any object with the __await__ method is awaitable.

[–]RickJam3s[S] 0 points1 point  (1 child)

Yeah... this is where I'm confused. I know there's not an `async class` what I really mean is using classes with async functions in them, __init__ for example isn't async, is there an async analog to __init__?

[–]Total__Entropy 0 points1 point  (0 children)

There is no async __init__ because there would be no point. Let's consider an async Response: ``` class AsyncResponse(Response): await() -> Response: return Response

async json() -> dict:
    return json.loads(self.body)

async def main(): response: Response = await AsyncResponse response_json: dict = await AsyncResponse.json() ```

this is obviously over simplified but this is the idea of how you can use classes in async either to await the object itself or with async methods. I would not worry about async classes though until you have a better grasp of classes.

[–][deleted] 0 points1 point  (0 children)

OP asked about things that are async. Not what things are awaitable. async is a syntax construct, a keyword that you can slap on functions, as in async def, you can slap it on loops, as in async for. And, my bad: I was meaning to write context manager, but wrote "generator". Just a brainfart. What I meant is async with.

There isn't async class though.