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


Groups > comp.lang.python > #16209

Re: Strange result ffor object to bool

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <zpengxen@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.04; 'c/c++': 0.05; 'python)': 0.05; 'versions,': 0.05; '"""': 0.07; 'none:': 0.07; 'subject:object': 0.07; 'versions.': 0.07; 'python': 0.08; 'properly.': 0.09; '25,': 0.12; 'library': 0.13; '4:59': 0.16; '5:06': 0.16; '>what': 0.16; 'boolean': 0.16; 'obj:': 0.16; 'received:209.85.213.174': 0.16; 'received:mail- yx0-f174.google.com': 0.16; 'subject:result': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve': 0.16; 'url:effbot': 0.16; 'url:zone': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'thanks,': 0.18; 'cc:no real name:2**0': 0.20; 'header:In-Reply-To:1': 0.22; 'cc:2**0': 0.24; 'fine': 0.24; 'tests': 0.25; '(in': 0.26; 'testing': 0.26; 'url:mailman': 0.28; 'right.': 0.28; 'message-id:@mail.gmail.com': 0.28; 'elements': 0.29; 'explicit': 0.29; 'rid': 0.29; 'true,': 0.29; 'print': 0.29; 'cc:addr:python.org': 0.29; 'nov': 0.29; 'pm,': 0.29; 'seem': 0.30; 'none,': 0.30; 'object.': 0.30; 'quoting': 0.30; 'least': 0.30; 'thanks': 0.31; 'url:listinfo': 0.32; 'fri,': 0.34; 'but,': 0.34; 'all.': 0.34; 'to:no real name:2**1': 0.35; 'test': 0.35; 'url:python': 0.36; '...': 0.36; 'none': 0.37; 'element': 0.37; 'instead.': 0.37; 'later,': 0.37; 'but': 0.37; 'run': 0.37; 'received:google.com': 0.37; 'think': 0.37; 'received:209.85': 0.38; 'url:org': 0.39; 'goes': 0.39; 'received:209': 0.40; 'to:2**2': 0.61; 'type': 0.61; '2011': 0.61; 'here': 0.65; 'course.': 0.66; 'roughly': 0.67; 'truth': 0.68; 'care': 0.71; 'evaluate': 0.71; 'children.': 0.74; "'if": 0.84; 'otten': 0.84; 'branch,': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=Sl5LDIxVZq2IdKzKY3MQLg2buqGuQJsAqx4hjxSgghU=; b=dNOQflg0qy2UZba3HrTI9jSxtAW4w/uFBVW/gKMfmWwnpY72zL/EuS9dp76a9BvKaa C+RQkgBc9yu6g+5H1XmDJYKXAmFxy7ka/A1NC4nbK4t8dotK7frpa1dDvgcNRDfW4aAq 1gbwYE1Qt7Z1mvqSK26VUHtzhEgVuy4CHWq1I=
MIME-Version 1.0
In-Reply-To <janlpd$8mm$1@dough.gmane.org>
References <CAAh7U5O-9u8b2=t3X8yfLdH_axEBoz1fQbr5=LTT2EYjoJVMvA@mail.gmail.com> <janlpd$8mm$1@dough.gmane.org>
Date Fri, 25 Nov 2011 18:09:43 +0800
Subject Re: Strange result ffor object to bool
From ZhouPeng <zpengxen@gmail.com>
To Peter Otten <__peter__@web.de>, steve@pearwood.info, rosuav@gmail.com
Content-Type text/plain; charset=windows-1252
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
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.3032.1322215786.27778.python-list@python.org> (permalink)
Lines 67
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1322215786 news.xs4all.nl 6883 [2001:888:2000:d::a6]:59459
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:16209

Show key headers only | View raw


Thanks all.

I am a c/c++ programer before,
So I directly think it is the same roughly between

if not obj: (in python) and if (!obj) {(in c/c++)

/ if obj: (in python) and if (obj) {(in c/c++)

That if obj is not None, 'if obj:' goes true branch, 'if not obj:'
goes false branch,
and I don't need to care where the obj is from (what type or what lib)

But, Now it seem not be consistent, so I feel strange.
And it seem be obj's library related in python.

On Fri, Nov 25, 2011 at 4:59 PM, Peter Otten <__peter__@web.de> wrote:
>What is a listen element? It is not a standard Python object. What
>library is it from?
from xml.etree.ElementTree

On Fri, Nov 25, 2011 at 5:06 PM, Peter Otten <__peter__@web.de> wrote:
> 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
Thanks,
> """
> 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.
> """
Yea,  you are right.
And I got it later, when I run my program in python 2.7.2,
It complains:
FutureWarning: The behavior of this method will change in future versions.
Use specific 'len(elem)' or 'elem is not None' test instead. if not graphics:
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
Zhou Peng

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


Thread

Re: Strange result ffor object to bool ZhouPeng <zpengxen@gmail.com> - 2011-11-25 18:09 +0800

csiph-web