Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #16208

Re: Strange result ffor object to bool

From Peter Otten <__peter__@web.de>
Subject Re: Strange result ffor object to bool
Followup-To gmane.comp.python.general
Date 2011-11-25 10:06 +0100
Organization None
References <CAAh7U5O-9u8b2=t3X8yfLdH_axEBoz1fQbr5=LTT2EYjoJVMvA@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3031.1322211967.27778.python-list@python.org> (permalink)

Followups directed to: gmane.comp.python.general

Show all headers | View raw


ZhouPeng wrote:

> In my program, I get a listen element by
> listen = graphics.find("listen")
> 
> print listen is <Element listen at 6afc20>
> print type listen is <type 'instance'>
> I am sure listen is not None and can be accessed properly.
> 
> But print bool(listen) is False
> if not listen  is True

bool(listen) is False here means that the Element has no children.

Quoting
http://effbot.org/zone/elementtree-13-intro.htm#truth-testing

"""
Truth testing #
The Element type now issues a warning when used in a “boolean context”. To 
get rid of the warning, make the test explicit:
if len(elem):
    ... has at least one children ...

elem = root.find("tag")
if elem is not None:
    ... found ...
Explicit tests work just fine in ET 1.2, of course.
The boolean interpretation will most likely change in future versions, so 
that all elements evaluate to true, also if they have no children.
"""


Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Strange result ffor object to bool Peter Otten <__peter__@web.de> - 2011-11-25 10:06 +0100

csiph-web