Python Glossary Русский

Python 3.5+inspectfunction

inspect.iscoroutinefunction()

coroutines and generators

Checks that an object is a function declared with async def (or marked with markcoroutinefunction).

Syntax

inspect.iscoroutinefunction(object) -> bool

The check looks at the code flag, not the behaviour: a plain function that returns a coroutine gives False, and so does an object with async def __call__. An async generator (async def with yield) is not a coroutine function. Since 3.14 asyncio.iscoroutinefunction is deprecated; use inspect.iscoroutinefunction. For an object rather than a function there are iscoroutine and isawaitable.

Examples

import asyncio, inspect
async def fetch():
    return 1
def fetch_later():
    return fetch()  # возвращает корутину, но сама обычная
class Handler:
    async def __call__(self):
        pass
print(inspect.iscoroutinefunction(fetch))  # → True
print(inspect.iscoroutinefunction(fetch_later))  # → False
print(inspect.iscoroutinefunction(Handler()))  # → False
print(asyncio.run(fetch_later()))  # → 1

See also

Open in the full glossarydocs.python.orgReport an inaccuracy