I tried using inspect.getsource(deque) and while it works on other modules from collections, running it on deque throws an error "could not find class definition". Why does this happen only to deque and how can I see it's source code?
Asked
Active
Viewed 2,077 times
7
-
It's not only for deque, you will probably see the same for other C implemented types such as `defaultdict`. – wim Feb 05 '19 at 00:29
-
Also, instead of `inspect.getsource` you may see the `__file__` attribute on the modules, and check the source files directly on your programing editor. However it won't work for `collections.deque` as it is defined in C, as per the answer given. – jsbueno Feb 05 '19 at 00:30
1 Answers
7
This one is implemented in C code for CPython, and the collections python module just imports that name. Depending on your Python version and installation, you may find somewhere on your system a file called _collections.so
or _collectionsmodule.so
which has the real implementation, but inspect.getsource
is not really smart enough to figure that out.
You can find the sources here:
https://github.com/python/cpython/blob/3.7/Modules/_collectionsmodule.c

wim
- 338,267
- 99
- 616
- 750