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


Groups > comp.lang.python > #16208

Re: Strange result ffor object to bool

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'versions,': 0.05; '"""': 0.07; 'none:': 0.07; 'subject:object': 0.07; 'properly.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'boolean': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'subject:result': 0.16; 'url:effbot': 0.16; 'url:zone': 0.16; 'wrote:': 0.18; 'from:addr:web.de': 0.23; 'fine': 0.24; 'tests': 0.25; 'testing': 0.26; 'elements': 0.29; 'explicit': 0.29; 'rid': 0.29; 'true,': 0.29; 'print': 0.29; 'quoting': 0.30; 'least': 0.30; 'header:X-Complaints-To:1': 0.33; 'to:addr:python- list': 0.34; 'test': 0.35; '...': 0.36; 'none': 0.37; 'element': 0.37; 'but': 0.37; 'received:org': 0.38; 'url:org': 0.39; 'to:addr:python.org': 0.40; 'type': 0.61; 'here': 0.65; 'course.': 0.66; 'truth': 0.68; 'evaluate': 0.71; 'children.': 0.74
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Strange result ffor object to bool
Followup-To gmane.comp.python.general
Date Fri, 25 Nov 2011 10:06:05 +0100
Organization None
References <CAAh7U5O-9u8b2=t3X8yfLdH_axEBoz1fQbr5=LTT2EYjoJVMvA@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Gmane-NNTP-Posting-Host p5084a7eb.dip.t-dialin.net
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3031.1322211967.27778.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1322211967 news.xs4all.nl 6986 [2001:888:2000:d::a6]:45466
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:16208

Followups directed to: gmane.comp.python.general

Show key headers only | 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