无在Python 2.6中键入问题

Vor*_*dok 2 python

我正在做一个脚本,我从数据库中获取一些值,但有时这个值可以是None,但是当我将它分配给变量并尝试比较它时,我得到这个错误:

TypeError: 'NoneType' object is unsubscriptable
Run Code Online (Sandbox Code Playgroud)

我已经尝试过了:

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' ] )
Run Code Online (Sandbox Code Playgroud)

而且他们都没有工作.

先感谢您.

小智 9

当您尝试对变量执行[]操作时,会出现unsubscriptable错误None.所以在这种情况下,很可能是sgSlate['sg_client_2']价值,而None不是sgSlate['sg_client_2']['name']自身.