help(f_fn)
例如sin
显示:
Help on function _lambdifygenerated:
_lambdifygenerated(x)
Created with lambdify. Signature:
func(x)
Expression:
sin(x)
Source code:
def _lambdifygenerated(x):
return sin(x)
在四处摸索之后,我发现映射定义了__globals__
这个函数:
In [224]: f_fn.__globals__.keys()
Out[224]: dict_keys(['sin', 'x', 'builtins', 'range', '__builtins__'])
In [225]: f_fn.__globals__['sin']
Out[225]: <function __main__.my_sin(x)>
您的“和”示例的帮助:
Help on function _lambdifygenerated:
_lambdifygenerated(x, y)
Created with lambdify. Signature:
func(x, y)
Expression:
x & y
Source code:
def _lambdifygenerated(x, y):
return x and y
虽然有类似的映射:
In [229]: f_and.__globals__.keys()
Out[229]: dict_keys(['and', 'y', 'x', 'builtins', 'range', '__builtins__'])
In [230]: f_and.__globals__['and']
Out[230]: <function __main__.my_and(x, y)>
但我认为这不起作用,因为and
inx and y
是 Python 关键字,是操作员角色,而不是函数。它不能映射到其他东西。
and
这是 Python的正常短路:
In [231]: f_and(False,'foo')
Out[231]: False
In [232]: f_and(True,'foo')
Out[232]: 'foo'
In [240]: f_and(x>0,y)
...
TypeError: cannot determine truth value of Relational
刚刚意识到我用过and
,而你用过And
:
In [241]: f_and = lambdify([x,y],f,{"And": my_and})
def _lambdifygenerated(x, y):
return x and y
In [242]: f_and.__globals__.keys()
Out[242]: dict_keys(['And', 'y', 'x', 'builtins', 'range', '__builtins__'])
In [243]: f_and.__globals__['And']
Out[243]: <function __main__.my_and(x, y)>
映射和函数之间的联系就更少了。
像这样的映射{"And": my_and}
不会直接影响代码翻译。那就是他们不生产
def foo(x,y):
return my_and(x,y) # if my_sin(x)
相反,它们决定了 Python 代码的运行方式。
如果我省略映射
f_and1 = lambdify([x,y], f)
代码是
def _lambdifygenerated(x,y):
return logical_and.reduce((x,y))
f_and1.__globals__
提供了一大堆numpy
功能,包括
In [254]: f_and1.__globals__['logical_and']
Out[254]: <ufunc 'logical_and'>
并且行为是numpy
明智的:
In [261]: f_and1([True,True],[True,False])
Out[261]: array([ True, False])
lambdify([x,y],f,[{"And": my_and},'numpy'])
还给我logical_and
翻译。