代码越简单越好
输出三个列表:
lsf为仅是函数不是方法的那些
lsm为仅是方法不是函数的那些
lsfm为既是函数又是方法的那些

如何用Python输出numpy中的所有函数和方法
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
关注
函数和方法是如何定义的呢?感觉题主说的是不同命名空间的问题吧?比如,一个ndarray对象有min()方法,但是在np的顶级命名空间中也存在np.min()函数。楼主是想区分这两类吗?实际上,np顶级命名空间有超过600个类或函数,每个类下又有少则几十多则上百的方法,把它们全部显示出来没有多少意义。如果题主一定要这样做,可以试试下面的方法。
先看看np顶级命名空间下的(太多了,仅显示前8个)
>>> for item in dir(np)[:8]: print(item) ALLOW_THREADS AxisError BUFSIZE Bytes0 CLIP ComplexWarning DataSource Datetime64 >>>
稍微改造一下这段代码,也可以只显式可以被调用的函数或方法。以np.ndarray对象为例。
>>> a = np.array([]) >>> for item in dir(a): if hasattr(a.__getattribute__(item), '__call__'): print(item) __abs__ __add__ __and__ __array__ __array_function__ __array_prepare__ __array_ufunc__ __array_wrap__ __bool__ __class__ __complex__ __contains__ __copy__ __deepcopy__ __delattr__ __delitem__ __dir__ __divmod__ __eq__ __float__ __floordiv__ __format__ __ge__ __getattribute__ __getitem__ __gt__ __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 byteswap choose clip compress conj conjugate copy cumprod cumsum diagonal dot dump dumps fill flatten getfield item itemset max mean min newbyteorder nonzero partition prod ptp put ravel repeat reshape resize round searchsorted setfield setflags sort squeeze std sum swapaxes take tobytes tofile tolist tostring trace transpose var view >>>
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用