2012-03-04 25 views
0

我有一個名爲entities.py文件,它包含以下代碼:python3對象沒有attriubute

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問題... 有沒有解決這個問題的方法?

回答

3

從發佈的代碼看起來您​​的縮進級別錯誤,您已在模塊上聲明show_all()方法,而不是類。

def_show_all(self):應縮進到同一水平entities = {}

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

    def show_all(self): 
     ...... 
+0

這是它!謝謝! – CosmicRabbitMediaInc 2012-03-04 23:51:29