Python:从数学模块函数创建新函数

时间:2018-10-07 01:24:15

标签: python

晚上好,我想使用x^3+x^2+sin(x)函数(例如),一个直观的尝试是这样的:

import math as m

h(x)=m.pow(x,3)+m.pow(x,2)+m.sin(x)

但是我得到一个SyntaxError: can't assign to function call

我该如何混合数学模块(或其他模块,没关系)以获得我需要的功能?

谢谢

1 个答案:

答案 0 :(得分:3)

最接近Python的示例是lambda。

import math as m

h = lambda x: m.pow(x,3)+m.pow(x,2)+m.sin(x)

但是lambda通常用于匿名函数。如果要命名,请改用def

import math as m

def h(x):
    return m.pow(x,3)+m.pow(x,2)+m.sin(x)