Test if function or method is normal or asynchronous
If you don't want to introduce another import with inspect
, iscoroutine
is also available inside asyncio
.
import asyncio
def isAsync(someFunc):
return asyncio.iscoroutinefunction(someFunc)
Use the inspect module of Python.
inspect.iscoroutinefunction(object)
Return true if the object is a coroutine function (a function defined with an async def syntax).
This function is available since Python 3.5. The module is available for Python 2 with lesser functionalities and certainly without the one you are looking for: inspect
Inspect module as the name suggests is useful to inspect a whole lot of thing. The documentation says
The inspect module provides several useful functions to help get information about live objects such as modules, classes, methods, functions, tracebacks, frame objects, and code objects. For example, it can help you examine the contents of a class, retrieve the source code of a method, extract and format the argument list for a function, or get all the information you need to display a detailed traceback.
There are four main kinds of services provided by this module: type checking, getting source code, inspecting classes and functions, and examining the interpreter stack.
Some basic capabilities of this module are:
inspect.ismodule(object)
inspect.isclass(object)
inspect.ismethod(object)
inspect.isfunction(object)
It also packs capability to retrieve the source code
inspect.getdoc(object)
inspect.getcomments(object)
inspect.getfile(object)
inspect.getmodule(object)
Methods are named intuitively. Description if needed can be found in documentation.