[BangPypers] ElementTree object instance evaluates to False?
saager.mhatre at gmail.com
Tue Dec 6 14:28:50 CET 2011
Refer https://gist.github.com/1327614#file_truth.py to see if it helps any.
On Tue, Dec 6, 2011 at 11:15 AM, Noufal Ibrahim <noufal at gmail.com> wrote:
> Vikas BN <vikas.bn at gmail.com> writes:
> > Anyone else encountered this or even have any insights regarding
> > this behaviour? I'm using Python2.7.2 on Linux
> > if that matters.
> ElementTree's `Element` objects behave this way. The "booleanness" is
> not conformant to the rest of Python.
> I don't know if or when it's been fixed but here's how to do it.
> The method you need to work with is `__nonzero__` if you want `bool`
> to work with your classes.
> With Python 2.7.2, The `Element` class defined in
> /usr/lib/python2.7/xml/etree/ElementTree.py has
> def __nonzero__(self):
> "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
> So, it will evaluate to False only if the number of children is
> Annoying but when I stumbled on this, it did make me aware of how
> consistent Python libraries generally are so that this stuck out.
>  http://docs.python.org/reference/datamodel.html#object.__nonzero__
> I am a deeply superficial person. -Andy Warhol
> BangPypers mailing list
> BangPypers at python.org
More information about the BangPypers