2013-02-19 79 views
0

我一直在xcode和vi上收到這個錯誤。 Python說行類LeastModel有一個IndentationError:期望一個縮進塊。 我在Xcode上檢查了我的偏好設置,使用4個空格作爲製表符,並且在任何地方使用了製表符。請幫幫我!Python IndentationError:Xcode上的「預期縮進塊」

def make_model(data,model): 

class LeastModel(): 
    """ 
    linear system solved using linear least squares 
    This class serves as an example that fulfills the model interface needed by the ransa function. 
    """ 
    def __init__(self,input_columns,output_columns): 
     self.input_columns = input_columns 
     self.output_columns = output_columns 
     #self.debug = debug 
+1

如果問題真的出現在您所說的「LeastModel類」行中,那麼問題必須出現在更早的行上(例如,是否有一個「if x:」行)? – 2013-02-19 20:17:27

回答

4

您的問題是你有行之後沒有縮進代碼:

def make_model(data,model): 

您可以:

  1. 擺脫該行

  2. 在該函數的主體中寫入一些縮進代碼

  3. 縮進整個類定義,以便在函數中定義類LeastModel

的事實判斷你叫你的函數make_model和你LeastModel類,我覺得你有點打算把函數內的類定義。但是這可能是一個錯誤 - 請注意,如果你在一個函數中定義了它,你將無法在該函數之外使用這個類(除非你從該函數返回類,return LeastModel

+0

好的,我只是在函數make_model的主體中添加了一個縮進的註釋,它的工作原理!謝謝! – snazziii 2013-02-19 20:29:46

2

假設沒有一個複製錯誤,這就是你的代碼實際上的樣子,你需要縮進__init__()所以它是類定義的內部:

class LeastModel(): 
    """ 
    linear system solved using linear least squares 
    This class serves as an example that fulfills the model interface needed by the ransa function. 
    """ 
    def __init__(self,input_columns,output_columns): 
     self.input_columns = input_columns 
     self.output_columns = output_columns 
     #self.debug = debug 

編輯:現在您已經包含了完整的代碼,問題實際上是您的make_model()函數定義下沒有任何內容。如果該功能實際上應該什麼都不做,請在def行下面添加pass(縮進一級)。否則,在那裏放一些代碼或刪除def行。

+1

這是真的,但它不會導致'IndentationError'(用Python 2.6,2.7和3測試),縮進的docstring和'pass'的作用相同,另外,OP說這是他的代碼看起來像的樣子無論如何 – 2013-02-19 20:19:44

1

它不應該是:

class LeastModel(): 
    """ 
    linear system solved using linear least squares 
    This class serves as an example that fulfills the model interface needed by the ransa function. 
    """ 
    def __init__(self,input_columns,output_columns): 
     self.input_columns = input_columns 
     self.output_columns = output_columns 
     #self.debug = debug 
+0

這是真的,但它不會導致'IndentationError'(用Python 2.6,2.7和3測試) – 2013-02-19 20:18:48

+0

對不起,是的,我確實有我的代碼,我會改變我的問題在stackoverflow – snazziii 2013-02-19 20:19:46

+0

我認爲這是因爲在LeastModel()之後沒有縮進代碼或'傳遞'。 – dln385 2013-02-19 20:20:14