IndentationError是否是Python中的语法错误?

时间:2022-03-27 22:42:07

I have a simple question,

我有一个简单的问题,

Is IndentationError a SyntaxError in Python or not?

IndentationError是否是Python中的SyntaxError?

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

我认为不是,但因为我是初学者,我想确定。语法错误只是那些在解释器中将SyntaxError作为响应的错误吗?例如,如果我输入

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?

所以,如果还有其他东西(IndentationError等),它可能是SyntaxError的子类型吗?

2 个解决方案

#1


12  

>>> issubclass(IndentationError, SyntaxError)
True

It means yes

这意味着是的

More info here and here

更多信息在这里和这里

#2


3  

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

您的示例是SyntaxError,因为您不能拥有以数字开头的标识符:

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

IndentationError是一种SyntaxError,请参阅以下方法中的方法解析顺序:help(IndentationError)和: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

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

#1


12  

>>> issubclass(IndentationError, SyntaxError)
True

It means yes

这意味着是的

More info here and here

更多信息在这里和这里

#2


3  

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

您的示例是SyntaxError,因为您不能拥有以数字开头的标识符:

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

IndentationError是一种SyntaxError,请参阅以下方法中的方法解析顺序:help(IndentationError)和: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

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