2017-04-14 5 views
0

xmlファイルされる:私はsraiのbool値をチェックするときPythonのElementTreeのXML解析:要素オブジェクトのbool値は、このような奇妙な

<aiml> 
<category> 
    <pattern>*test*</pattern> 
    <template><srai>test</srai></template> 
</category> 
</aiml> 

トラブルはそれが偽

(Pdb) not srai_tag 
True 
(Pdb) srai_tag 
<Element 'srai' at 0x10e08fea8> 
(Pdb) not template 
False 
(Pdb) template 
<Element 'template' at 0x10e08fe58> 

私を返し、ありますPythonのバージョンは3.6.1です。

+0

クラスは自由にTrue/Falseを定義できます。 'Element'クラスがそれをどのように定義しているか知っていますか? –

+0

ああ、将来のバージョンでは '__bool__'メソッドを変更します。 –

答えて

1

クラスxml.etree.ElementTree.Elementを確認しましたが、将来変更されます。 John Gordonのおかげでありがとう。

def __bool__(self): 
    warnings.warn(
     "The behavior of this method will change in future versions. " 
     "Use specific 'len(elem)' or 'elem is not None' test instead.", 
     FutureWarning, stacklevel=2 
     ) 
    return len(self._children) != 0 # emulate old behaviour, for now 
関連する問題