python3对象没有属性

时间:2012-03-04 23:38:45

标签: python-3.x

我有一个名为entities.py的文件,其中包含以下代码:

class EntityClass: 
    entities = {}


def __init__(self, parent=None):
    .......


def show_all(self):
    ......

但是,当我运行python 3并按如下方式键入命令时:

>>> import entities
>>> ent = entities.EntityClass()
>>> ent.show_all()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'EntityClass' object has no attribute 'show_all'

show_all显然应该是EntityClass的一个属性。 这当然在Python 2中完美运行,我假设这是一个Python 3问题...... 有解决方法吗?

1 个答案:

答案 0 :(得分:3)

从发布的代码中,看起来你的缩进级别是错误的,你已经在模块上声明了show_all()方法,而不是类。

def_show_all(self):应缩进到与entities = {}

相同的级别
class EntityClass: 
    entities ={}
    def __init__(self,parent=None):
        .......

    def show_all(self):
        ......