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.
These are defined in multiarraymodule.c:
https://github.com/numpy/numpy/blob/820765d762513510a8e46f108e8bc8b366127f8f/numpy/core/src/multiarray/multiarraymodule.c#L4279
array
function in Python is _array_fromobject()
in C, and arange
function in Python is array_arange()
in C.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With