Home python Built-in Python Functions

Built-in Python Functions




How can I see the source code of the built-in functions?

Tried to connect the inspect module and calling the getsource function, set the function to me as the parameter. But the method of the specified module refused to return the source code for the built-in features, but, for example, if the getsource function print function, then the errors will not and I can get what I wanted.

Answer 1, Authority 100%

As wrote Kovadim, many built-in functions of python are written on C. But, since Python is an open source language, then the source code can be viewed here . If you are interested in the implementation of the same modules on python, I advise you to see Pypy sources .

p.s. To find out which file refers to which module, you can usually see the __ file __ .

p.p.s. There is Similar Question in English Stack.

Answer 2, Authority 100%

The “Built-in Function” phrase in an error message means that the corresponding function is not implemented on pure python:

& gt; & gt; & gt; Import Inspect
& gt; & gt; & gt; Import NUMPY.
& gt; & gt; & gt; INSPECT.GETSOURCE (PRINT)
Traceback (MOST Recent Call Last):
TypeRorror: & lt; Built-in Function Print & GT; IS NOT A MODULE, Class, Method, Function, Traceback, Frame, Or Code Object
& gt; & gt; & gt; Inspect.GetSource (numpy.array)
Traceback (MOST Recent Call Last):
Typeerror: & lt; Built-in Function Array & GT; IS NOT A MODULE, Class, Method, Function, Traceback, Frame, Or Code Object

Obviously, numpy.array is not an “built-in function” in the sense of belonging to __ Builtins __ namespace , and is” built-in function “in the sense” is not implemented on pure python. “

Source code of such functions should be signed in the respective projects: CPYthon’s print () , pypy , Jython’s print () , NUMPY. Array () , NUMPY FOR PYPY .

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions