22
私はデータ構造としてリストから継承したクラスを使用しています:Pythonで 'to boolean'演算子をオーバーライドしますか?
class CItem(list) :
pass
oItem = CItem()
oItem.m_something = 10
oItem += [ 1, 2, 3 ]
すべてが完璧ですが、私は内部の私のクラスの私のオブジェクトを使用する場合は、「が」、PythonはFalseにそれを評価し、その根底に要素がない場合2.xで
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素晴らしいPythonの宝石を発見したようです –