Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x python3对象没有属性_Python 3.x - Fatal编程技术网

Python 3.x python3对象没有属性

Python 3.x python3对象没有属性,python-3.x,Python 3.x,我有一个名为entities.py的文件,其中包含以下代码: class EntityClass: entities = {} def __init__(self, parent=None): ....... def show_all(self): ...... 但是,当我运行python 3并键入如下命令时: >>> import entities >>> ent = entities.EntityClass() >

我有一个名为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'
导入实体 >>>ent=entities.EntityClass() >>>ent.show_all() 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 AttributeError:“EntityClass”对象没有属性“show\u all”
show\u all
显然应该是EntityClass的一个属性。 这在Python2中当然非常有效,我假设这是Python3的问题。。。
有解决方法吗?

从发布的代码来看,您的缩进级别似乎是错误的,您在模块上声明了show_all()方法,而不是类

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

class EntityClass: 
    entities ={}
    def __init__(self,parent=None):
        .......

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

从发布的代码来看,缩进级别似乎是错误的,您在模块上声明了show_all()方法,而不是类

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

class EntityClass: 
    entities ={}
    def __init__(self,parent=None):
        .......

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