I want to print the source code of a built-in method. for example, math is a built-in module of python and i want to print the source code of ceil
i know how to print the source code of a custom module using inspect.getsource
need help
I am trying to create a programme where i can call any builtin methods or functions and it will display only the source code of that function or module.
Python has almost everything in builtin library, i want to use these libraries
example:
input: factorial
output:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
works just fine
import inspect
inspect.getsource(factorial)
doesn't work ... results in Type Error
import inspect
import math
print(inspect.getsource(math.ceil)
TypeError: <built-in function ceil> is not a module, class, method, function, traceback, frame, or code object
thanks in advance :)
ipythonyou can use thefunction??magic to see the source code - if it is written in Python. But if compiled from C code it will be marked as builtin. Usually I look online for the development repository (e.g. github) But matching the Python function name with the C function name can be tricky.math.ceilincpython/Modules/mathmodule.cthat looks like it's the source.grepto search the C source code.