覆盖';到布尔值';python中的运算符?

覆盖';到布尔值';python中的运算符?,python,Python,我使用从列表继承的类作为数据结构: class CItem( list ) : pass oItem = CItem() oItem.m_something = 10 oItem += [ 1, 2, 3 ] 所有这些都是完美的,但是如果我在“if”中使用我的类的对象,那么如果列表底层没有元素,python会将其计算为False。因为我的类不仅仅是列表,我真的希望它只有在无时才计算为False,否则计算为True: a = None if a : print "this is not

我使用从列表继承的类作为数据结构:

class CItem( list ) :
  pass
oItem = CItem()
oItem.m_something = 10
oItem += [ 1, 2, 3 ]
所有这些都是完美的,但是如果我在“if”中使用我的类的对象,那么如果列表底层没有元素,python会将其计算为False。因为我的类不仅仅是列表,我真的希望它只有在无时才计算为False,否则计算为True:

a = None
if a :
  print "this is not called, as expected"
a = CItem()
if a :
  print "and this is not called too, since CItem is empty list. How to fix it?"

在2.x中:覆盖。在3.x中,覆盖。

+1因为您似乎发现了另一个奇妙的Python宝石:)