3 ms·
One thing I don't really get, why is the "async" keyword necessary at all? Couldn't just the mere presence of "await" in the function body implicitly make the f
by Spiritus 11y ago
One thing I don't really get, why is the "async" keyword necessary at all? Couldn't just the mere presence of "await" in the function body implicitly make the function "async"?
- balls187 11y agoSelf documenting code for one. I suppose you could follow the .NET Naming convention of appending "Async" to all routines.
- skrebbel 11y agoI guess the reason is that it changes the type of the return value. Compare: def moo(): return 5 async def oink(): return 5 moo returns 5, oink returns an awaitable. For a human, without the 'async' marker, you'd have to scan the entire function source to know. I guess also type checkers and documentation tools like to have an idea about the return type.
- Spiritus 11y agoAh right, that makes sense.
- kevin_thibedeau 11y agoThe presence of "yield" also changes the return type. No extra keywords needed for that.
- svieira 11y agoThis is directly addressed in the PEP [1] - the short of it is, without the `async` modifier if you have two functions: def important(): await something() def something(): # ... stuff ... await something_else() # ... more stuff ... and you refactor `await something_else()` into a new method then you've changed the return type of `something` from `Awaitable[ReturnType]` to just `ReturnType` and `important` will break at run-time when it tries to `await something()`. [1]: https://www.python.org/dev/peps/pep-0492/#importance-of-async-keyword