【问题标题】:Python IndentationError: “expected an indented block” on XcodePython IndentationError:Xcode 上的“预期有缩进块”
【发布时间】:2013-02-19 20:15:15
【问题描述】:

我在 xcode 和 vi 上遇到了这个错误。 Python 说行类 LeastModel 有一个 IndentationError: expected an indented block。 我检查了我在 Xcode 上的偏好,以使用 4 个空格作为选项卡以及我一直在使用选项卡的所有地方。请帮帮我!

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

【问题讨论】:

  • 如果问题真的是你所说的class LeastModel 行,那么问题一定出在前面的行上(例如,在它之前有if x: 行吗?)

标签: python xcode indentation


【解决方案1】:

你的问题是你在行后没有缩进代码:

def make_model(data,model):

您可以:

  1. 去掉那一行

  2. 将一些缩进代码写入该函数的主体

  3. 缩进整个类定义,以便在函数中定义类LeastModel

从你调用你的函数make_model 和你的类LeastModel 的事实来看,我认为你以某种方式打算将类定义放在函数中。但这可能是您的错误 - 请注意,如果您在函数中定义它,您将无法在函数外部使用该类(除非您从函数返回类本身,使用 @987654325 行@。

【讨论】:

  • 好的,我刚刚在我的函数 make_model 的主体中添加了一个缩进的注释,它起作用了!谢谢!
【解决方案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 行。

【讨论】:

  • 没错,但不会导致IndentationError。 (在 Python 2.6、2.7 和 3 中测试)。缩进的文档字符串与pass 的作用相同。此外,OP 说这就是他的代码的样子。
【解决方案3】:

不应该是:

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

【讨论】:

  • 没错,但不会导致IndentationError。 (在 Python 2.6、2.7 和 3 中测试)。
  • 对不起,是的,我确实有这样的代码。我将在 stackoverflow 上更改我的问题
  • 我认为会是因为LeastModel(): 之后没有缩进代码或pass
  • @dln385:试试看。 (缩进的文档字符串与pass 的作用相同)。
  • 我尝试在 LeastModel() 类下面添加 pass,但我仍然得到同样的错误
猜你喜欢
  • 2017-09-19
  • 1970-01-01
  • 1970-01-01
  • 2014-12-15
  • 1970-01-01
  • 1970-01-01
  • 2015-10-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多