Python 3.x python3对象没有属性
我有一个名为entities.py的文件,其中包含以下代码: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() >
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):
......