6

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?

10
  • Quick tip: if it says <built-in function whatever>, that means it's in C, not Python. You're not going to find a def statement for it. Commented May 8, 2018 at 4:34
  • numpy is compiled C under the hood is that really what you are after? Commented May 8, 2018 at 4:34
  • @Julien: I don't think there's any C++. It's Python, C, and Fortran. Commented May 8, 2018 at 4:35
  • 1
    type(np.arange) will show builtin as opposed tofunction. Commented May 8, 2018 at 4:42
  • 2
    While this question is not great, it has legitimate non-trivial answers that demonstrate the relative difficulty some beginners have in digging through the numpy code. Commented May 8, 2018 at 4:45

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

2 Comments

A followup if you don't mind: why is it we can refer 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.
@FreshAir, use the name that's given in the docs. It all comes down to how various modules are imported. Sometimes import numpy as np is enough. Sometimes you have to use something like from scipy import sparse.
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.