2015-04-01 69 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 

並丟棄功能()方法