3

Why all the functions listed in __all__ (of the init file) of numpy.random subpackage work even though they aren't defined there? I can imagine that they are written in C, but still when I do

from numpy.random import *

why doesn't Python throw an error since neither they are defined there (in the init file) nor is there seemingly any mechanism in the file that does the required imports.

ankit
  • 3
  • 1
  • 11
  • 3
    The `__init__.py` has a `from .mtrand import *` import. `np.random.mtrand` is a compiled `.so` file. The `ipython` tab completion for `np.random` looks the same as that for `np.random.mtrand`. – hpaulj Feb 02 '21 at 07:28
  • The answer is mostly C extensions and Cython importing from compiled files. – Dschoni Feb 02 '21 at 07:32
  • Thanks a lot. I think the lengthy name of mtrand file was the reason it was getting eluded for days from my eyes. – ankit Feb 02 '21 at 07:39
  • @hpaulj btw these 'so' files are new to me, so I was left wondering what are all the file extensions accepted by Python. Can you kindly let me know? – ankit Feb 02 '21 at 07:44
  • and if random functions are defined in 'mtrand', what is the role of 'randomkit.h'? – ankit Feb 02 '21 at 07:49
  • Filetypes depend on the platform. There might be .pyd and .pxd files (e.g. on windows) as well as .so files on *nix. https://cython.readthedocs.io/en/latest/src/tutorial/pxd_files.html – Dschoni Feb 02 '21 at 07:51
  • @Dschoni Thanks for the reply, but isn't this '.pxd' extension a Cython thing instead of Python? – ankit Feb 02 '21 at 08:05
  • 1
    Yes it is. A lot of numpy modules are C extensions and therefore use Cython as an interface to Python. – Dschoni Feb 02 '21 at 08:11
  • 2
    In `https://github.com/numpy/numpy/tree/master/numpy/random` there's a cython source, a *.pyx`. Then compiled and linked it becomes a `.so` binary. – hpaulj Feb 02 '21 at 08:13
  • 1
    And instead of `.so`, on Windows it becomes `.pyd`. See more here: https://cython.readthedocs.io/en/latest/src/quickstart/build.html – Dschoni Feb 02 '21 at 08:17
  • @hpaulj would you mind writing your comment as an answer – ankit May 28 '21 at 09:15

0 Answers0