Python 分析在Numpy数组上使用dir()的结果

Python 分析在Numpy数组上使用dir()的结果,python,arrays,numpy,Python,Arrays,Numpy,我有一个numpy数组a 当我在numpy数组上使用dir()时,如下所示: dir(a) 我得到以下列表: '__abs__', '__add__', '__and__', '__array__', '__array_finalize__', '__array_function__', '__array_interface__', '__array_prepare__', '__array_priority__', '__array_struct__', '__arra

我有一个numpy数组a

当我在numpy数组上使用dir()时,如下所示:

dir(a)
我得到以下列表:

 '__abs__',
 '__add__',
 '__and__',
 '__array__',
 '__array_finalize__',
 '__array_function__',
 '__array_interface__',
 '__array_prepare__',
 '__array_priority__',
 '__array_struct__',
 '__array_ufunc__',
 '__array_wrap__',
 '__bool__',
 '__class__',
 '__complex__',
 '__contains__',
 '__copy__',
 '__deepcopy__',
 '__delattr__',
 '__delitem__',
 '__dir__',
 '__divmod__',
 '__doc__',
 '__eq__',
 '__float__',
 '__floordiv__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__gt__',
 '__hash__',
 '__iadd__',
 '__iand__',
 '__ifloordiv__',
 '__ilshift__',
 '__imatmul__',
 '__imod__',
 '__imul__',
 '__index__',
 '__init__',
 '__init_subclass__',
 '__int__',
 '__invert__',
 '__ior__',
 '__ipow__',
 '__irshift__',
 '__isub__',
 '__iter__',
 '__itruediv__',
 '__ixor__',
 '__le__',
 '__len__',
 '__lshift__',
 '__lt__',
 '__matmul__',
 '__mod__',
 '__mul__',
 '__ne__',
 '__neg__',
 '__new__',
 '__or__',
 '__pos__',
 '__pow__',
 '__radd__',
 '__rand__',
 '__rdivmod__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__rfloordiv__',
 '__rlshift__',
 '__rmatmul__',
 '__rmod__',
 '__rmul__',
 '__ror__',
 '__rpow__',
 '__rrshift__',
 '__rshift__',
 '__rsub__',
 '__rtruediv__',
 '__rxor__',
 '__setattr__',
 '__setitem__',
 '__setstate__',
 '__sizeof__',
 '__str__',
 '__sub__',
 '__subclasshook__',
 '__truediv__',
 '__xor__',
 'all',
 'any',
 'argmax',
 'argmin',
 'argpartition',
 'argsort',
 'astype',
 'base',
 'byteswap',
 'choose',
 'clip',
 'compress',
 'conj',
 'conjugate',
 'copy',
 'ctypes',
 'cumprod',
 'cumsum',
 'data',
 'diagonal',
 'dot',
 'dtype',
 'dump',
 'dumps',
 'fill',
 'flags',
 'flat',
 'flatten',
 'getfield',
 'imag',
 'item',
 'itemset',
 'itemsize',
 'max',
 'mean',
 'min',
 'nbytes',
 'ndim',
 'newbyteorder',
 'nonzero',
 'partition',
 'prod',
 'ptp',
 'put',
 'ravel',
 'real',
 'repeat',
 'reshape',
 'resize',
 'round',
 'searchsorted',
 'setfield',
 'setflags',
 'shape',
 'size',
 'sort',
 'squeeze',
 'std',
 'strides',
 'sum',
 'swapaxes',
 'take',
 'tobytes',
 'tofile',
 'tolist',
 'tostring',
 'trace',
 'transpose',
 'var',
 'view']
现在,我有两个问题:

  • 有没有办法自动将属性和方法从上面的列表中分离出来?我的意思是有一个方法可以只输出numpy数组的方法列表或属性列表

  • 列表中有dunder方法___;,但由于dir(a)而输出的列表中没有len()。为什么?

  • 列表中有dunder方法___;,但由于dir(a)而输出的列表中没有len()。为什么?

    \uuuu len\uuuu()
    实现
    len()

    有没有办法自动将属性和方法从上面的列表中分离出来?我的意思是有一个方法可以只输出numpy数组的方法列表或属性列表

    对于属性,可以执行
    vars(a)

    对于您可以执行的方法:

    methods = [method_name for method_name in dir(a)
                      if callable(getattr(a, method_name))]
    
    在上面的代码片段中,您将通过检查
    object.method\u name
    是否可调用来创建表示方法名称的字符串列表

    如果需要非dunder方法,则可以添加以下修改:

    def is_dunder(name):
        if (
            len(name) > 4 and 
            name[:2] == name[-2:] == '__'
        ):
            return True
        else:
            return False
    
    non_dunder_methods = [
        method_name for method_name in a if 
        not is_dunder(method_name) and callable(getattr(a, method_name))
    ]
    

    1.由于
    ndarray
    是由编译后的代码构建的,因此属性和方法之间的区别不像用Python编写类那样清晰。还有许多属性是属性,或者类似属性,这意味着使用它们实际上调用了一个方法。所有公共文件都有记录:。
    的用户通常不会直接使用。Python将
    len(x)
    转换为
    x.
    调用,
    +
    生成
    \uuuuuuuuuuuuuuuuu
    或'iadd'等调用。
    itpython
    完成工具(选项卡)分别列出所有这些项(“uuuuuuuuuuuuuuuuuuuuu”),并附加一个
    ()
    to methods。我正在学习numpy,因此我认为最好获取与numpy数组关联的所有属性和方法。如果我使用vars(),我会得到以下错误:“vars()参数必须具有u_______;属性”。上面是从对象获取属性和方法的方法。然而,我没有意识到@hpaulj所指出的numpy对象的细微差别——所以我同意他们的答案。