0

I am trying to evaluate some Python commands in SublimeREPL but every new command causes an 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.

>>> 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. I tried looking everywhere for this answer.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
Bruce Pucci
  • 1,821
  • 2
  • 19
  • 26

1 Answers1

0

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. Maybe because of the formatting issues here or something.

However, in Python, an empty line indicates that you're done with the previous section. This only matters when using the interactive interpreter (like in 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. Then, it's seeing the def __copy__(self), and noticing that it has some space indenting. This is causing the IndentationError you're receiving.

Either comment on the already existing bug about this in SublimeREPL's github account, or don't use empty lines in your code.

VooDooNOFX
  • 4,674
  • 2
  • 23
  • 22