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


Groups > comp.lang.python > #19192

Re: while True or while 1

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <landimatte@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.028
X-Spam-Evidence '*H*': 0.94; '*S*': 0.00; 'interpreter': 0.05; 'subject:while': 0.07; 'def': 0.13; 'cc:addr:python-list': 0.15; '"while': 0.16; 'boolean': 0.16; 'invalidate': 0.16; 'matteo': 0.16; 'wrote:': 0.16; 'cc:no real name:2**0': 0.21; 'maybe': 0.21; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'wrong?': 0.23; 'cc:2**0': 0.25; 'sender:addr:gmail.com': 0.25; 'code': 0.25; 'url:mailman': 0.26; 'tried': 0.27; 'fact': 0.27; 'looks': 0.27; 'pass': 0.28; 'true,': 0.28; 'cc:addr:python.org': 0.29; 'logic': 0.30; 'url:listinfo': 0.30; 'anyone': 0.31; 'that,': 0.32; 'header :User-Agent:1': 0.33; 'instead': 0.33; 'there': 0.33; 'loop': 0.34; 'probably': 0.35; '...': 0.35; 'url:python': 0.36; 'equal': 0.36; '(to': 0.36; 'charset:us-ascii': 0.36; 'but': 0.37; 'received:google.com': 0.37; 'using': 0.37; 'received:209.85': 0.38; 'think': 0.38; 'sometimes': 0.38; 'url:org': 0.39; 'why': 0.39; 'received:209': 0.39; 'received:78': 0.39; 'subject:: ': 0.39; 'difference': 0.40; 'url:net': 0.61; 'more': 0.61; 'presented': 0.62; 'below:': 0.80; 'andrea': 0.84; '13)': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=sender:date:from:to:cc:subject:message-id:references:mime-version :content-type:content-disposition:in-reply-to:user-agent; bh=8tOc2l4fXb3kg8ZJADDKQciTHB/+lP1wfQhhJc0ytm0=; b=kxNOIAklEFHZNAirWjsxit2ibxOr/lDaC7azbVa312xAd0nAH3KaG+OBHw90ViW5mx iLoHau4jVSe/nc0WAi7DmaLOiJeUt/i7pdsv1x7j+cL/yYPRyTiy6pE9fULa+/v29ryD dDGSuZJDiBHw0D9kXmvvs9K6jyejApnq3R+oM=
Sender Matteo Landi <landimatte@gmail.com>
Date Sat, 21 Jan 2012 16:24:09 +0100
From Matteo Landi <matteo@matteolandi.net>
To Andrea Crotti <andrea.crotti.0@gmail.com>
Subject Re: while True or while 1
References <4F1AC1D4.2080402@gmail.com>
MIME-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Disposition inline
In-Reply-To <4F1AC1D4.2080402@gmail.com>
User-Agent Mutt/1.5.21 (2010-09-15)
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.4910.1327159453.27778.python-list@python.org> (permalink)
Lines 57
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1327159453 news.xs4all.nl 6940 [2001:888:2000:d::a6]:41926
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:19192

Show key headers only | View raw


Probably because of the fact it is possible to set True equal to False and
consequently then invalidate loop logic as presented below:

    True = False
    while True:
        ...

On the other hand `1' will always be evaluated as a constant.

Don't know, just guessing.


Matteo

On Jan/21, Andrea Crotti wrote:
> I see sometimes in other people code "while 1" instead of "while True".
> I think using True is more pythonic, but I wanted to check if there is
> any difference in practice.
> 
> So I tried to do the following, and the result is surprising.  For what
> I can see it looks like the interpreter can optimize away the 1 boolean
> conversion while it doesn't with the True, the opposite of what I
> supposed.
> 
> Anyone can explain me why is that, or maybe is my conclusion wrong?
> 
>   def f1():
>       while 1:
>           pass
> 
>   def f2():
>       while True:
>           pass
> 
>   In [10]: dis.dis(f)
>   2           0 SETUP_LOOP               3 (to 6)
> 
>   3 >>    3 JUMP_ABSOLUTE            3
> >>    6 LOAD_CONST               0 (None)
>               9 RETURN_VALUE
> 
>   In [9]: dis.dis(f1)
>   2           0 SETUP_LOOP              10 (to 13)
> >>    3 LOAD_GLOBAL              0 (True)
>               6 POP_JUMP_IF_FALSE       12
> 
>   3           9 JUMP_ABSOLUTE            3
> >>   12 POP_BLOCK
> >>   13 LOAD_CONST               0 (None)
>              16 RETURN_VALUE
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 

-- 
http://www.matteolandi.net

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


Thread

Re: while True or while 1 Matteo Landi <matteo@matteolandi.net> - 2012-01-21 16:24 +0100

csiph-web