【问题标题】:Is IndentationError a syntax error in Python or not?IndentationError 是否是 Python 中的语法错误?
【发布时间】:2013-10-15 07:29:07
【问题描述】:

我有一个简单的问题,

IndentationError 在 Python 中是不是 SyntaxError

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

3f = 22 

我明白了

SyntaxError: invalid syntax 

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

【问题讨论】:

    标签: python python-3.x syntax


    【解决方案1】:
    >>> issubclass(IndentationError, SyntaxError)
    True
    

    是的意思

    更多信息herehere

    【讨论】:

    • 谢谢!你的回答很有帮助!
    【解决方案2】:

    你的例子是一个 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
    

    IndentationError 是 SyntaxError 的一种,方法解析顺序见:help(IndentationError) 和:http://docs.python.org/2/library/exceptions.html#exceptions.IndentationError

    有效标识符:

    test
    test3
    test_3
    __3Test_3______
    

    无效的标识符:

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

    另见:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      • 2014-07-22
      • 1970-01-01
      • 1970-01-01
      • 2015-09-10
      • 1970-01-01
      相关资源
      最近更新 更多