IndentationError เป็นข้อผิดพลาดทางไวยากรณ์ใน Python หรือไม่

ฉันมีคำถามง่ายๆว่า

IndentationError a SyntaxError อยู่ใน Python หรือไม่?

ฉันคิดว่ามันไม่ใช่แต่เนื่องจากฉันเป็นมือใหม่ฉันจึงอยากจะแน่ใจ ข้อผิดพลาดทางไวยากรณ์เป็นเพียงข้อผิดพลาดที่ให้ฉัน SyntaxError เป็นคำตอบในล่ามหรือไม่ เช่น ถ้าฉันพิมพ์

3f = 22 

ฉันเข้าใจ

SyntaxError: invalid syntax 

ดังนั้นหากมีอย่างอื่น (IndentationError ฯลฯ) อาจเป็นประเภทย่อยของ SyntaxError หรือไม่?


person SomeOne    schedule 15.10.2013    source แหล่งที่มา


คำตอบ (2)


>>> issubclass(IndentationError, SyntaxError)
True

มันหมายความว่าใช่

ข้อมูลเพิ่มเติม ที่นี่ และ ที่นี่

person alexvassel    schedule 15.10.2013
comment
ขอบคุณ! คำตอบของคุณมีประโยชน์มาก! - person SomeOne; 15.10.2013

ตัวอย่างของคุณคือ 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#Exceptionions.IndentationError

ตัวระบุที่ถูกต้อง:

test
test3
test_3
__3Test
3f
333
33__
# Using any symbol other than: _
_____

ตัวระบุไม่ถูกต้อง:

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

ดูสิ่งนี้ด้วย:

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

person HarmonicaMuse    schedule 15.10.2013