For code implemented in Python (as opposed to C extensions), if you're using ipython
, an easy way to see the source code is to use the ??
operator. For example, on my 3.6 install:
In [1]: import asyncio
In [2]: asyncio.sleep??
Signature: asyncio.sleep(delay, result=None, *, loop=None)
Source:
@coroutine
def sleep(delay, result=None, *, loop=None):
"""Coroutine that completes after a given time (in seconds)."""
if delay == 0:
yield
return result
if loop is None:
loop = events.get_event_loop()
future = loop.create_future()
h = future._loop.call_later(delay,
futures._set_result_unless_cancelled,
future, result)
try:
return (yield from future)
finally:
h.cancel()
File: c:\program files\python36\lib\asyncio\tasks.py
Type: function
You can also just look at the CPython GitHub repo, but depending on the code organization it may not be obvious where to look (e.g. in this case the code actually exists in asyncio.tasks
, and is auto-imported into asyncio
), while ipython
magic finds it for you directly.