1

So in an iPython terminal, if you type a method followed by ?? you will get the source code and its location:

In [1]: import numpy as np

In [2]: np.angle??

Source:
@array_function_dispatch(_angle_dispatcher)
def angle(z, deg=False):
    z = asanyarray(z)
    if issubclass(z.dtype.type, _nx.complexfloating):
        zimag = z.imag
        zreal = z.real
    else:
        zimag = 0
        zreal = z

    a = arctan2(zimag, zreal)
    if deg:
        a *= 180/pi
    return a
File:      ~/Documents/venv_global/lib/python3.8/site-packages/numpy/lib/function_base.py
Type:      function

How would I find the location of the source code (+the line number of the method) without entering into iPython.

PS: (Removed the docstring from the output for readability).

Edit:

I am looking for something like a subcommand that I can run which would give the information without needing to enter the shell. Not sure if it is possible though but it would look something like this:

ipython --source numpy.angle
Al-Baraa El-Hag
  • 770
  • 6
  • 15
  • Does this answer your question? [How can I get the source code of a Python function?](https://stackoverflow.com/questions/427453/how-can-i-get-the-source-code-of-a-python-function) – TankorSmash Nov 20 '20 at 19:20
  • @TankorSmash That was the question that I looked at before posting. All of them require you to enter a terminal. – Al-Baraa El-Hag Nov 20 '20 at 19:57
  • What do you mean? You need to run python to execute python code. `inspect.getsource(foo) ` gives you the source, no? – TankorSmash Nov 20 '20 at 20:01
  • @TankorSmash I was looking for a subcommand that I can add to iPython that will give it to me without having to enter the shell. For example: `ipython --source numpy.angle`. Though I do not know if it is possible – Al-Baraa El-Hag Nov 20 '20 at 20:03

1 Answers1

1

What you are seeing is a docstring and source documentations and file path:

import inspect
import numpy as np

print(np.angle.__doc__)
print('Source:')
print(inspect.getsource(np.angle))
print('File:')
print(inspect.getsourcefile(np.angle))
print('Type:')
print(type(np.angle))

The above code prints the docstrings, the source file path and type you get from ?? in iPython.

Prayson W. Daniel
  • 14,191
  • 4
  • 51
  • 57