Where can I find numpy.where() source code? [duplicate]

2024/11/15 22:05:36

I have already found the source for the numpy.ma.where() function but it seems to be calling the numpy.where() function and to better understand it I would like to take a look if possible.

Answer

Most Python functions are written in the Python language, but some functions are written in something more native (often the C language).

Regular Python functions ("pure Python")

There are a few techniques you can use to ask Python itself where a function is defined. Probably the most portable uses the inspect module:

>>> import numpy
>>> import inspect
>>> inspect.isbuiltin(numpy.ma.where)
False
>>> inspect.getsourcefile(numpy.ma.where)
'.../numpy/core/multiarray.py'

But this won't work with a native ("built-in") function:

>>> import numpy
>>> import inspect
>>> inspect.isbuiltin(numpy.where)
True
>>> inspect.getsourcefile(numpy.where)
TypeError: <built-in function where> is not a module, class, method, function, traceback, frame, or code object

Native ("built-in") functions

Unfortunately, Python doesn't provide a record of source files for built-in functions. You can find out which module provides the function:

>>> import numpy as np
>>> np.where
<built-in function where>
>>> np.where.__module__
'numpy.core.multiarray'

Python won't help you find the native (C) source code for that module, but in this case it's reasonable to look in the numpy project for C source that has similar names. I found the following file:

numpy/core/src/multiarray/multiarraymodule.c

And in that file, I found a list of definitions (PyMethodDef) including:

    {"where",(PyCFunction)array_where,METH_VARARGS, NULL},

This suggests that the C function array_where is the one that Python sees as "where".

The array_where function is defined in the same file, and it mostly delegates to the PyArray_Where function.

In short

NumPy's np.where function is written in C, not Python. A good place to look is PyArray_Where.

https://en.xdnf.cn/q/71633.html

Related Q&A

NSUserNotificationCenter.defaultUserNotificationCenter() returns None in python

I am trying to connect to the Mountain Lion notification center via python. Ive installed pyobjc and am following the instructions here and here. Also see: Working with Mountain Lions Notification Cent…

Flask app hangs while processing the request

I have a simple flask app, single page, upload html and then do some processing on it on the POST; at POST request; i am using beautifulsoup, pandas and usually it takes 5-10 sec to complete the task. …

How do I make a server listen on multiple ports

I would like to listen on 100 different TCP port with the same server. Heres what Im currently doing:-import socket import selectdef main():server_socket = socket.socket(socket.AF_INET, socket.SOCK_STR…

Django REST Framework: return 404 (not 400) on POST if related field does not exist?

Im developing a REST API which takes POST requests from some really brain-dead software which cant PATCH or anything else. The POSTs are to update Model objects which already exist in the database.Spec…

How can i login in instagram with python requests?

Hello i am trying to login instagram with python requests library but when i try, instagram turns me "bad requests". İs anyone know how can i solve this problem?i searched to find a solve f…

How to abstract away command code in custom django commands

Im writing custom django commands under my apps management/commands directory. At the moment I have 6 different files in that directory. Each file has a different command that solves a unique need. How…

Python one-liner (converting perl to pyp)

I was wondering if its possible to make a one-liner with pyp that has the same functionality as this.perl -l -a -F, -p -eif ($. > 1) { $F[6] %= 12; $F[7] %= 12;$_ = join(q{,}, @F[6,7]) }This takes i…

Getting symbols with Lark parsing

Im trying to parse a little pseudo-code Im writing and having some trouble getting values for symbols. It parses successfully, but it wont return a value the same as it would with "regular" …

Why cant I connect to my localhost django dev server?

Im creating a django app and in the process of setting up my local test environment. I can successfully get manage.py runserver working but pointing my browser to any variation of http://127.0.0.1:8000…

How to use L2 pooling in Tensorflow?

I am trying to implement one CNN architecture that uses L2 pooling. The reference paper particularly argues that L2 pooling was better than max pooling, so I would like to try L2 pooling after the acti…