I was looking for the source code for the arange and array functions in NumPy, but I couldn't find it: https://github.com/numpy/numpy/search?utf8=%E2%9C%93&q=%22def+arange%22+path%3Anumpy%2Fcore&type=
Could anyone enlighten me?
I was looking for the source code for the arange and array functions in NumPy, but I couldn't find it: https://github.com/numpy/numpy/search?utf8=%E2%9C%93&q=%22def+arange%22+path%3Anumpy%2Fcore&type=
Could anyone enlighten me?
numpy.array and numpy.arange are written in C. You can tell because they say "built-in" when you look at them:
>>> numpy.array
<built-in function array>
>>> numpy.arange
<built-in function arange>
That means there's no def statement. Instead, we look at what module they come from:
>>> numpy.array.__module__
'numpy.core.multiarray'
>>> numpy.arange.__module__
'numpy.core.multiarray'
navigate to the corresponding source file, and take a look at the array controlling the module's exported functions:
{"array",
(PyCFunction)_array_fromobject,
METH_VARARGS|METH_KEYWORDS, NULL},
...
{"arange",
(PyCFunction)array_arange,
METH_VARARGS|METH_KEYWORDS, NULL},
numpy.array and numpy.arange correspond to _array_fromobject and array_arange in that file. That's not where all the work happens, though. You'll need to keep digging to find all the relevant code.
arange simply as numpy.arange, instead of numpy.core.multiarray.arange? It seems to work with nonzero as well, which is not a builtin: numpy.nonzero just works, no need to say numpy.core.nonzero. However, this is not always possible, as matplotlib.plot does not work, and we have to say matplotlib.pyplot.plot.import numpy as np is enough. Sometimes you have to use something like from scipy import sparse.These are defined in multiarraymodule.c:
array function in Python is _array_fromobject() in C, and arange function in Python is array_arange() in C.
<built-in function whatever>, that means it's in C, not Python. You're not going to find adefstatement for it.type(np.arange)will showbuiltinas opposed tofunction.