2011-04-02 47 views
2

我做一個腳本,我得到一些值從數據庫中,但有時這個值可以是無,但是當我把它分配給一個變量,並嘗試比較它,我得到這個錯誤:在Python無類型問題2.6

TypeError: 'NoneType' object is unsubscriptable 

我已經嘗試過這樣的:

if sgSlate[ 'sg_client_2' ][ 'name' ] != None: 
    self.ui.brandComboBox_2.setEditText(sgSlate[ 'sg_client_2' ]['name' ]) 

if not isinstanceof(sgSlate[ 'sg_client_2' ][ 'name' ], None) != "": 
    self.ui.brandComboBox_2.setEditText(sgSlate[ 'sg_client_2' ]['name' ]) 

if sgSlate[ 'sg_client_2' ][ 'name' ] is not None: 
    self.ui.brandComboBox_2.setEditText(sgSlate[ 'sg_client_2' ]['name' ]) 

if type(sgSlate[ 'sg_client_2' ][ 'name' ]) is not type(None): 
    self.ui.brandComboBox_2.setEditText(sgSlate[ 'sg_client_2' ]['name' ]) 

,並沒有一次成功。

預先感謝您。

回答

9

的unsubscriptable錯誤,當您嘗試在None變量進行[]操作出現。所以在這種情況下,最有可能的sgSlate['sg_client_2']值是None,而不是sgSlate['sg_client_2']['name']本身。

+0

比你非常努力 – Vordok 2011-04-02 01:39:06

+1

好的,就是這樣。直到4月2日在所有時區都沒有更多的投票,我厭倦了這些愚蠢的獨角獸:) – 2011-04-02 02:18:32