如何打印 "def" 语句中定义的函数表达式
How to print expression of a function defined in "def" statement
我正在使用一个通过二分法计算函数根的小程序(即在 f(x)=0
时找到 x
)。 (我承认我从一些 SO
post 那里偷了这个程序,但我不能提供参考,也不能感谢作者,因为我不记得它在哪里......)
我的输出是:
Root is: -0.567143
Delta with 0 is: 0.000000000166
我也想输出我正在处理的函数的表达式,就像def f(x)
中写的那样:
Function is: x + np.exp(x)
Root is: -0.567143
Delta with 0 is: 0.000000000166
我不知道该怎么做...?可以修改 def f(x)
以将函数写入某处吗?或者是否有必要在别处进行另一种类型的操作?
我的程序是:
def samesign(a, b):
return a * b > 0
def dichotomy(func, low, high, tolerance=None):
# func(low) and func(high) must have opposite signs
assert not samesign(func(low), func(high)), ("\n"
"Assertion failed!\n"
"Replace low or high")
condition = True
while condition:
midpoint = (low + high) / 2.0
if samesign(func(low), func(midpoint)):
low = midpoint
else:
high = midpoint
if tolerance is not None and abs(high - low) < tolerance:
break
condition = abs(high - low) > tolerance
return midpoint
def f(x):
# Define function
return x + np.exp(x)
x = dichotomy(f, -1, 0, 1E-9)
import inspect
line_return = inspect.getsourcelines(f)[0][-1]
llr = len(line_return)
name = line_return[11 : llr-1]
print(f'Function is: {name}')
print(f'Root is: {x: .6f}')
print(f'Delta with 0 is: {f(x): .12f}')
如How can I get the source code of a Python function?, you can get the full definition of the function with the inspect
模块所示。
>>> import inspect
>>> print(inspect.getsource(f))
def f(x):
# Define function
return x + np.exp(x)
注意函数 f
must be defined in a source file.
如果您只对return语句的内容感兴趣,您必须探索ast of the function to extract the return statement using of the ast
模块。
import ast
def get_return_statement(fct):
root = ast.parse(inspect.getsource(f))
try:
return_node = next(
node for node in ast.walk(root) if isinstance(node, ast.Return)
)
return ast.unparse(return_node.value)
except StopIteration:
return "None"
get_return_statement(f)
会给你 x + np.exp(x)
.
为 3.9 之前的 python 编辑:
ast.unparse
仅在 python 3.9 之后可用。可以用 astor
third party library, by replacing ast.unparse
with astor.to_source
.
代替
括号多了结果略有不同:(x + np.exp(x))
.
我正在使用一个通过二分法计算函数根的小程序(即在 f(x)=0
时找到 x
)。 (我承认我从一些 SO
post 那里偷了这个程序,但我不能提供参考,也不能感谢作者,因为我不记得它在哪里......)
我的输出是:
Root is: -0.567143
Delta with 0 is: 0.000000000166
我也想输出我正在处理的函数的表达式,就像def f(x)
中写的那样:
Function is: x + np.exp(x)
Root is: -0.567143
Delta with 0 is: 0.000000000166
我不知道该怎么做...?可以修改 def f(x)
以将函数写入某处吗?或者是否有必要在别处进行另一种类型的操作?
我的程序是:
def samesign(a, b):
return a * b > 0
def dichotomy(func, low, high, tolerance=None):
# func(low) and func(high) must have opposite signs
assert not samesign(func(low), func(high)), ("\n"
"Assertion failed!\n"
"Replace low or high")
condition = True
while condition:
midpoint = (low + high) / 2.0
if samesign(func(low), func(midpoint)):
low = midpoint
else:
high = midpoint
if tolerance is not None and abs(high - low) < tolerance:
break
condition = abs(high - low) > tolerance
return midpoint
def f(x):
# Define function
return x + np.exp(x)
x = dichotomy(f, -1, 0, 1E-9)
import inspect
line_return = inspect.getsourcelines(f)[0][-1]
llr = len(line_return)
name = line_return[11 : llr-1]
print(f'Function is: {name}')
print(f'Root is: {x: .6f}')
print(f'Delta with 0 is: {f(x): .12f}')
如How can I get the source code of a Python function?, you can get the full definition of the function with the inspect
模块所示。
>>> import inspect
>>> print(inspect.getsource(f))
def f(x):
# Define function
return x + np.exp(x)
注意函数 f
must be defined in a source file.
如果您只对return语句的内容感兴趣,您必须探索ast of the function to extract the return statement using of the ast
模块。
import ast
def get_return_statement(fct):
root = ast.parse(inspect.getsource(f))
try:
return_node = next(
node for node in ast.walk(root) if isinstance(node, ast.Return)
)
return ast.unparse(return_node.value)
except StopIteration:
return "None"
get_return_statement(f)
会给你 x + np.exp(x)
.
为 3.9 之前的 python 编辑:
ast.unparse
仅在 python 3.9 之后可用。可以用 astor
third party library, by replacing ast.unparse
with astor.to_source
.
括号多了结果略有不同:(x + np.exp(x))
.