0

I've seen this question:

How can I get the source code of a Python function?

But I can't find anything that works generically for built-in functions and user-defined functions.

eg, I try:

>>> print.__repr__()
'<built-in function print>'

and

>>> repr(print)
'<built-in function print>'

and

>>> print(print)
<built-in function print>

and

$ python
Python 3.6.4 |Anaconda custom (64-bit)| (default, Jan 16 2018, 18:10:19) 
>>> inspect.getsource(print)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/anaconda3/lib/python3.6/inspect.py", line 968, in getsource
    lines, lnum = getsourcelines(object)
  File "/home/user/anaconda3/lib/python3.6/inspect.py", line 955, in getsourcelines
    lines, lnum = findsource(object)
  File "/home/user/anaconda3/lib/python3.6/inspect.py", line 768, in findsource
    file = getsourcefile(object)
  File "/home/user/anaconda3/lib/python3.6/inspect.py", line 684, in getsourcefile
    filename = getfile(object)
  File "/home/user/anaconda3/lib/python3.6/inspect.py", line 666, in getfile
    'function, traceback, frame, or code object'.format(object))
TypeError: <built-in function print> is not a module, class, method, function, traceback, frame, or code object

How can I do this?

Update:

Duplication police: this isn't a duplicate. I'm obviously literally citing and acknowledging the question this is allegedly a duplicate of in the very first line of this question and asking for something expanding on that.

Mittenchops
  • 18,633
  • 33
  • 128
  • 246
  • you can use `inspect` for python function signature https://docs.python.org/3.5/library/inspect.html#inspect.signature – Ami Hollander Dec 27 '18 at 09:46
  • Built-in functions weren't written in python though: https://stackoverflow.com/questions/8608587/finding-the-source-code-for-built-in-python-functions – TerryA Dec 27 '18 at 09:48
  • 2
    There is no way to get the source of built-in functions implemented in C, since their source code is not present in the Python distribution, nor is there an index to C source code locations for the function. You need to inspect the cpython repository and find the function yourself. – Amadan Dec 27 '18 at 09:48
  • Thank. @Amadan are there any tools that do this operation automatically? – Mittenchops Dec 27 '18 at 09:50
  • 1
    I don't believe this is possible, unless someone goes and writes such a mapping. And I don't believe anyone has. – Amadan Dec 27 '18 at 09:53
  • By the way, here's [`print` source](https://github.com/python/cpython/blob/62be74290aca26d16f3f55ece7ff6dad14e60e8d/Python/bltinmodule.c#L1816) that you wanted. – Amadan Dec 27 '18 at 10:02
  • [This link](https://devguide.python.org/exploring/) found in the dupe is awesome for digging through CPython sources. – Amadan Dec 27 '18 at 10:05
  • Oh neat, the AST documentation alone is worth it. Thanks! Sorry I didn't see the original when I posted this, I can normally count on google-fu and stackoverflow's similar questions to surface it, but didn't see with my query. – Mittenchops Dec 27 '18 at 10:06

0 Answers0