2015-04-01 74 views
-3

我有以下代码:不能分配到函数调用(Python)的

class Quadratic: 

    def __init__(self, a, b, c): 
     self.a = a 
     self.b = b 
     self.c = c 

    def function(self, x): 
     self.F(x) = a*(x**2) + b*x + c 
     return F(x) 

    def table(self, lower, upper, varz): 
     inc = np.absolute(upper-lower)/varz 
     print inc 
     for i in range(0 , varz - 1): 
      self.F(lower) 
      lower = lower + inc 
     #lower bound,upper bound, number of values 

    def roots(): 
     x1 = (-b + math.sqrt(b**2 - 4*a*c))/(2*a) 
     x2 = (-b - math.sqrt(b**2 - 4*a*c))/(2*a) 
     return x1, x2 

    def dump(self): 
     print self.n 

每当我尝试运行此脚本,我得到以下几点:

line 15 
self.F(x) = a*(x**2) + b*x + c 
SyntaxError: can't assign to function call 

任何帮助或想法将不胜感激。

+2

有什么解释?你不能分配给一个函数调用。如果你想***定义一个函数,使用'def'或'lambda'。 – MattDMo 2015-04-01 21:46:56

+0

什么是'F'?目前还不清楚你想要做什么。 – frnhr 2015-04-01 21:47:28

+0

什么是'self.F(x)'? – 2015-04-01 21:49:17

回答

2

目前尚不清楚(至少对我来说)它是什么你想达到的,但也许它是如此简单:

def F(self, x): 
    return self.a * (x ** 2) + self.b * x + c 
1

正如你可能已经猜到了,F(x)是一个函数呼叫。您正在调用函数F,该函数实际上并不存在,并将参数x传递给该函数,该函数实际上并不存在。你需要给你的变量合法名称,如f_of_x

1

尝试:

def __init__(self, a,b,c): 
    self.a = a 
    self.b = b 
    self.c = c 
    self.F = lambda x: a*(x**2)+b*x+c 

并丢弃功能()方法