2009-04-17 65 views
22

我使用的是从列表中继承的数据结构类:在Python中重写'布尔'操作符?

class CItem(list) : 
    pass 
oItem = CItem() 
oItem.m_something = 10 
oItem += [ 1, 2, 3 ] 

一切都是完美的,但如果我用我的我的类的对象内部的“如果”,蟒蛇评估为FALSE如果基础列表中没有元素。因为我的课不只是名单,我真的希望它评估假,只有当它是无,和评价,否则真:

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?" 
+1

+1因为你似乎发现自己另一个美妙的Python宝石:) – 2009-08-14 21:35:28

回答