我现在在做的是:
import numpy as np
eps = np.finfo(float).eps
def sindiv(x):
x = np.abs(x)
return np.maximum(eps, np.sin(x)) / np.maximum(eps, x)
Run Code Online (Sandbox Code Playgroud)
但是还有很多额外的阵列操作.有没有更好的办法?
我知道inspect.getargspec可以用来获取函数参数的名称:
>>> from inspect import getargspec
>>> def foo1(a, b):
... pass
...
>>> getargspec(foo1).args
['a', 'b']
Run Code Online (Sandbox Code Playgroud)
但以下并不是我所期望的:
>>> class X(object):
... def foo2(self, a, b):
... pass
...
>>> x = X()
>>> getargspec(x.foo2).args
['self', 'a', 'b']
Run Code Online (Sandbox Code Playgroud)
并且:
>>> from functools import partial
>>> def foo3(a, b, c):
... pass
...
>>> foo4 = partial(foo3, c=1)
>>> getargspec(foo4).args
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\inspect.py", line 816, in getargspec
raise TypeError('{!r} …Run Code Online (Sandbox Code Playgroud)