简体   繁体   English

Python IndentationError:使用SublimeREPL升华文本2

[英]Python IndentationError: Sublime Text 2 using SublimeREPL

I am trying to evaluate some Python commands in SublimeREPL but every new command causes an IndentationError. 我正在尝试在SublimeREPL中评估一些Python命令,但是每个新命令都会导致IndentationError。 The code works if I send it one line at a time. 如果我一次发送一行代码,则该代码有效。

Here is an attempt at an example... 这是一个例子。

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)

which evaluates to... 评估为...

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

However if I put some line comment characters in before each line it works fine except for the trailing "... ... ... ..." 但是,如果我在每行之前放置一些行注释字符,则除了尾随的“ ... ... ... ...”外,它都可以正常工作

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)
#

After it has been sent I have to switch over to the REPL window and hit enter on the line with the "... ... ... ..." for it to be evaluated. 发送后,我必须切换到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)
#
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 

I am new to Python so I apologize if I am missing something very simple. 我是Python的新手,如果我错过了一些非常简单的内容,我深表歉意。 I tried looking everywhere for this answer. 我试图到处寻找这个答案。

I suspect you've shown us code that's not exactly like what you're really running. 我怀疑您向我们展示了与您实际运行的代码不完全相同的代码。 In your actual python script, you've probably got a few newlines separating your def's from each other (which is fine), but they aren't in the pasted section of code here. 在您实际的python脚本中,您可能已经有一些换行符将def彼此分开(这很好),但是它们不在此处的代码粘贴部分中。 Maybe because of the formatting issues here or something. 可能是因为此处存在格式问题。

However, in Python, an empty line indicates that you're done with the previous section. 但是,在Python中,空行表示您已完成上一节。 This only matters when using the interactive interpreter (like in SublimeREPL). 这仅在使用交互式解释器时才重要(例如在SublimeREPL中)。 Spaces and Indentation matter. 空格和缩进很重要。 In this case, it's just transferring your text to the interpreter, which is the same thing that happens if you just cut and paste it yourself. 在这种情况下,它只是将您的文本传输到解释器,如果您自己剪切并粘贴它,就会发生同样的事情。 It's seeing the newline before def __copy__(self) , and assuming it should evaluate that class definition. 它在def __copy__(self)之前看到换行符,并假定它应该评估该类定义。 Then, it's seeing the def __copy__(self) , and noticing that it has some space indenting. 然后,看到def __copy__(self) ,并注意到它具有一定的空间缩进。 This is causing the IndentationError you're receiving. 这导致您收到IndentationError

Either comment on the already existing bug about this in SublimeREPL's github account, or don't use empty lines in your code. 请评论SublimeREPL的github帐户中有关此问题的现有错误 ,或者不要在代码中使用空行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM