3

I have a simple question,

Is IndentationError a SyntaxError in Python or not?

I think it is not but since I am a beginner I would like to be sure. Are syntax errors only those which give me SyntaxError as a response in an interpreter? For example, if I type

3f = 22 

I get

SyntaxError: invalid syntax 

So if there's something else (IndentationError etc), may it be a sub-type of SyntaxError or not?

Santosh Kumar
  • 26,475
  • 20
  • 67
  • 118
SomeOne
  • 469
  • 2
  • 6
  • 16

2 Answers2

14
>>> issubclass(IndentationError, SyntaxError)
True

It means yes

More info here and here

alexvassel
  • 10,600
  • 2
  • 29
  • 31
3

Your example is a SyntaxError, because you can't have an identifier that starts with a number:

>>> 3f = 22
  File "<stdin>", line 1
    3f = 22
     ^
SyntaxError: invalid syntax


>>>     f3 = 22
  File "<stdin>", line 1
    f3 = 22
    ^
IndentationError: unexpected indent


>>> def test():
... f3 = 22
  File "<stdin>", line 2
    f3 = 22
     ^
IndentationError: expected an indented block

An IndentationError is a kind of SyntaxError, see the method resolution order in: help(IndentationError) and: http://docs.python.org/2/library/exceptions.html#exceptions.IndentationError

Valid identifiers:

test
test3
test_3
__3Test_3______

Invalid identifiers:

3f
333
33__
# Using any symbol other than: _

See also:

http://docs.python.org/2/reference/lexical_analysis.html#identifiers

HarmonicaMuse
  • 7,633
  • 37
  • 52