dopexxx
dopexxx

Reputation: 2636

Python command that returns location to a function

I know this sounds stupid.

I am looking for python command that returns the path to the location of a function. So like

command(np.reshape)

should give the path to the folder in which np.reshape is located. I am NOT interested in finding the main location of the entire module (here numpy), this has been answered repeatedly. I need the precise path to the function itself, exactly like which is doing it in MATLAB.

Upvotes: 0

Views: 63

Answers (1)

Ankit Jaiswal
Ankit Jaiswal

Reputation: 23427

I believe you are looking for inspect.getsourcefile()

As per the documentation, inspect.getsourcefile(object)

Return the name of the Python source file in which an object was defined. This will fail with a TypeError if the object is a built-in module, class, or function.

Here is what I get when tested with join function:

>>> import inspect
>>> from os.path import join
>>> inspect.getsourcefile(join)
'C:\\Users\\ankit\\...\\Python\\Python36\\lib\\ntpath.py'

Upvotes: 3

Related Questions