0

我正在尝试评估 SublimeREPL 中的一些 Python 命令,但每个新命令都会导致 IndentationError。如果我一次发送一行,该代码就可以工作。

这是一个示例的尝试...

class Array(object):
    def __init__(self, length = 0, baseIndex = 0):
        assert(length >= 0)
        self._data = [0 for i in range(length)]
        self._baseIndex = baseIndex
    def __copy__(self):
        result = Array(len(self._data))
        for i, datum in enumerate(self._data):
            result._data[i] = datum
        result._baseIndex = self._baseIndex
        return result
    def __len__(self):
        return len(self._data)

评估为...

IndentationError: unexpected indent
>>> class Array(object):
...     def __init__(self, length = 0, baseIndex = 0):
...         assert(length >= 0)
...         self._data = [0 for i in range(length)]
...         self._baseIndex = baseIndex
... 
>>>     def __copy__(self):
  File "<stdin>", line 1
    def __copy__(self):
    ^
IndentationError: unexpected indent
>>>         result = Array(len(self._data))
  File "<stdin>", line 1
    result = Array(len(self._data))
    ^
IndentationError: unexpected indent
>>>         for i, datum in enumerate(self._data):
  File "<stdin>", line 1
    for i, datum in enumerate(self._data):
    ^
IndentationError: unexpected indent
>>>             result._data[i] = datum
  File "<stdin>", line 1
    result._data[i] = datum
    ^
IndentationError: unexpected indent
>>>         result._baseIndex = self._baseIndex
  File "<stdin>", line 1
    result._baseIndex = self._baseIndex
    ^
IndentationError: unexpected indent
>>>         return result
  File "<stdin>", line 1
    return result
    ^
IndentationError: unexpected indent
>>> 
>>>     def __len__(self):
  File "<stdin>", line 1
    def __len__(self):
    ^
IndentationError: unexpected indent
>>>         return len(self._data)
  File "<stdin>", line 1
    return len(self._data)
    ^
IndentationError: unexpected indent

但是,如果我在每行之前添加一些行注释字符,它可以正常工作,除了尾随的“... ... ... ...”

class Array(object):
    def __init__(self, length = 0, baseIndex = 0):
        assert(length >= 0)
        self._data = [0 for i in range(length)]
        self._baseIndex = baseIndex
#
    def __copy__(self):
        result = Array(len(self._data))
        for i, datum in enumerate(self._data):
            result._data[i] = datum
        result._baseIndex = self._baseIndex
        return result
#
    def __len__(self):
        return len(self._data)
#

发送后,我必须切换到 REPL 窗口并在带有“... ... ... ...”的行上按 Enter 以对其进行评估。

>>> class Array(object):
    def __init__(self, length = 0, baseIndex = 0):
        assert(length >= 0)
        self._data = [0 for i in range(length)]
        self._baseIndex = baseIndex
#
    def __copy__(self):
        result = Array(len(self._data))
        for i, datum in enumerate(self._data):
            result._data[i] = datum
        result._baseIndex = self._baseIndex
        return result
#
    def __len__(self):
        return len(self._data)
#
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 

我是 Python 新手,所以如果我遗漏了一些非常简单的东西,我深表歉意。我试着到处寻找这个答案。

4

1 回答 1

0

我怀疑您向我们展示的代码与您实际运行的代码不完全相同。在您的实际 python 脚本中,您可能有一些换行符将您的 def 彼此分开(这很好),但它们不在此处粘贴的代码部分中。也许是因为这里的格式问题或其他原因。

但是,在 Python 中,空行表示您已完成上一节。这仅在使用交互式解释器(如在 SublimeREPL 中)时才重要。空格和缩进很重要。在这种情况下,它只是将您的文本传输到解释器,如果您只是自己剪切和粘贴它会发生同样的事情。它在 before 看到换行符def __copy__(self),并假设它应该评估该类定义。然后,它看到def __copy__(self),并注意到它有一些空格缩进。这导致IndentationError您收到。

要么评论SublimeREPL 的 github 帐户中已经存在的错误,要么不要在代码中使用空行。

于 2014-10-25T01:46:22.323 回答