Python - IndentationError:预期缩进块(涉及类的异常缩进错误)

时间:2017-01-01 05:16:57

标签: python class

在尝试开发课程时,我遇到了这个错误。

from __future__ import division
import numpy as np
import scipy as sp
import itertools as it
from scipy.integrate import quad
import astropy.cosmology
from astropy import units as u

class NFW:  

File "/Users/alexandres/Illustris/Scripts/NFWprofile2.py", line 10

               ^
IndentationError: expected an indented block
[Finished in 0.1s with exit code 1]
[shell_cmd: python -u "/Users/alexandres/Illustris/Scripts/NFWprofile2.py"]
[dir: /Users/alexandres/Illustris/Scripts]
[path: /usr/bin:/bin:/usr/sbin:/sbin]

这是一个缩进错误?

无论我将课程定义为NFW()还是NFW(object),都会发生这种情况。

这是通过Sublime 3

进行编辑的

2 个答案:

答案 0 :(得分:5)

如果这是您的整个文件,则您缺少所需的课程正文。您可以使用pass语句创建一个空体:

class NFW:  
    pass

答案 1 :(得分:1)

发生错误是因为您有一个没有语句的块。

即,class NFW为空。

用于创建最小类,

class MyEmptyClass:
        pass

在你的情况下

class NFW:  
    pass

pass语句不执行任何操作。当语法需要语句但程序不需要操作时,可以使用它。