2009-04-17 54 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

回答