Answering to add more information than the dupe target that vaultah provided.
The following answer addresses 3.x directly, I noticed you're still on 2.x. For a good write-up on that, check out this answer.
You're actually down the right path on this, but the issue is that print
is a built-in, so inspect.getsource
won't do you much good here.
Which is to say:
>>> inspect.getsource.__doc__
'Return the text of the source code for an object.
The argument may be a module, class, method, function, traceback, frame,
or code object. The source code is returned as a single string. An
OSError is raised if the source code cannot be retrieved.'
and where as print
is of type
:
>>> type(print)
<class 'builtin_function_or_method'>
And more specifically:
>>> print.__module__
'builtins'
how unfortunate, it's not supported by getsource
.
You have options:
1) Walk through the Python source code and see how your built-in has been implemented. In my case, I almost always use CPython, so I'd start at the CPython directory.
Since we know we're hunting for a builtin
module, we go into the /Python
dir and look for something that looks like it would contain built-in modules. bltinmodule.c
is a safe guess. Knowing that print has to be defined as a function to be callable, search for print(
and we hop right to builtin_print(Pyobject... where it's defined.
2) Make the lucky guess of the built-in function name convention and search for builtin_print
in the code repo.
3) Use a tool that does the magic behind the scenes, such as Puneeth Chaganti's Cinspect.