python的包含有的属性是哪来的

如numpy,我看了github上numpy的包结构,发现numpy就是一个包,但其__init__.py文件并没有ones()之类的函数定义,也没有对其的import引用,那么为什么我import numpy之后,可以写numpy.ones()来调用这个函数呢。

阅读 2.2k
2 个回答

numpy/__init__.py里有一句:

from .core import *

然后core/__init__.py里有一句:

from .numeric import *

用help哦,就可以看到位置在 numpy.core.numeric

help(numpy.ones)
Help on function ones in module numpy.core.numeric:

ones(shape, dtype=None, order='C')
    Return a new array of given shape and type, filled with ones.
    
    Parameters
    ----------
    shape : int or sequence of ints
        Shape of the new array, e.g., ``(2, 3)`` or ``2``.
    dtype : data-type, optional
        The desired data-type for the array, e.g., `numpy.int8`.  Default is
        `numpy.float64`.
    order : {'C', 'F'}, optional
        Whether to store multidimensional data in C- or Fortran-contiguous
        (row- or column-wise) order in memory.
    
    Returns
    -------
    out : ndarray
        Array of ones with the given shape, dtype, and order.
    
    See Also
    --------

clipboard.png

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题