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


Groups > comp.lang.python > #16208 > unrolled thread

Re: Strange result ffor object to bool

Started byPeter Otten <__peter__@web.de>
First post2011-11-25 10:06 +0100
Last post2011-11-25 10:06 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#16208 — Re: Strange result ffor object to bool

FromPeter Otten <__peter__@web.de>
Date2011-11-25 10:06 +0100
SubjectRe: Strange result ffor object to bool
Message-ID<mailman.3031.1322211967.27778.python-list@python.org>
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.
"""


[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web