Question

Whenever I run my code it gives me a SyntaxError that I don't understand and always points back to the second line of this code:

gen_item = '''
def__getitem__(self,index):
    if type(index) == str:
        if index not in self._fields:
            raise IndexError("Index matches no fields.")
        else:
            return self.index
    elif type(index) == int:
        if index > len(self._fields) - 1:
            raise IndexError("Index out of range.")
        else:
            return self._fields[index]\n'''

with the message "File "< string>" line 19". I'm not sure what the problem is seeing as this is all one big triple-quoted string. I'm using Python 3.3.

Was it helpful?

Solution

The SyntaxError is being raised by wherever you are execing that string. Thusly:

In [209]: s = '''
     ...: blah blah blah syntax error
     ...: 
     ...: blah blah'''

In [210]: exec(s)
  File "<string>", line 2
    blah blah blah syntax error
            ^
SyntaxError: invalid syntax

Your actual syntax error is this:

def__getitem__(self,index):

You're missing a space in between def and __getitem__.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top