0

I can get the source code of a function using inspect.getsource(obj).

print(inspect.getsource(gcd))

It prints the source code of the gcd function. When I try the following, it throws error.

>>>print(inspect.getsource(print))

  File "<stdin>", line 1
     print(inspect.getsourcelines(print))
                                 ^
  SyntaxError: invalid syntax

Can I get the source code of print ?; If yes How?, If no Why?

vaultah
  • 44,105
  • 12
  • 114
  • 143
Venkatesh
  • 1,537
  • 15
  • 28
  • 4
    No, because `print` is a function, written in C. `inspect` can only get sources of Python functions. – vaultah Jan 07 '15 at 09:07
  • 1
    You are aware that python is open source and thus the whole source code is available online, right? You can just get it straight from the repository instead of using `inspect` (which throws a syntax error as `print` is a language keyword in python 2, not a function as it is in python 3). – l4mpi Jan 07 '15 at 09:08
  • @l4mpi Ya, I know python is open source. So only I tried to view the source code of print. – Venkatesh Jan 07 '15 at 09:10
  • 1
    @Martin Pieters It is not a duplicate of that question. I can't get the source code of print. I want to just see the implementation of print not all the in-built functions and I want a reason for that error. How it could be a duplicate of that question? – Venkatesh Jan 07 '15 at 09:17
  • 2
    possible duplicate of [Finding the source code for built-in Python functions?](http://stackoverflow.com/questions/8608587/finding-the-source-code-for-built-in-python-functions) – vaultah Jan 07 '15 at 09:33
  • @Venkatesh I did give you a reason for the error: `print` is a _keyword_ in python2, not just a function name; and thus it can't occur as an argument to anything. The same error would happen if you call `getsource` with any other keyword like `if`, `for`, `with`, `and` etc. And see the duplicate linked by vaultah for how to find the source (the answer by keviarpe specifically). Lastly, here's a link to the c source: https://hg.python.org/releasing/2.7.9/file/753a8f457ddc/Python/bltinmodule.c#l1569 – l4mpi Jan 07 '15 at 10:22
  • 1
    I'll ask a different question: why? What are you trying to find out about `print`? By the way, you *can* use the print function in python 2 using the `from __future__ import print_function` but it behaves differently to the `print` statement. – cdarke Jan 07 '15 at 10:53
  • Also related: [Where builtin functions are implemented](http://stackoverflow.com/q/24723583), [C Code for Python's property function?](http://stackoverflow.com/q/16447280) and [Python source code for built-in "in" operator](http://stackoverflow.com/q/12244074) – Martijn Pieters Jan 07 '15 at 11:14

1 Answers1

5

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.

Community
  • 1
  • 1
  • Your answer is valid for Python 3.x, but the OP is using Python 2.7, in which `print` is a statement. – bruno desthuilliers Jan 07 '15 at 10:24
  • @brunodesthuilliers yeah, I noticed that after posting. I have to run to handle something important and will edit later. –  Jan 07 '15 at 10:28