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


Groups > comp.lang.python > #19188

while True or while 1

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <andrea.crotti.0@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.042
X-Spam-Evidence '*H*': 0.92; '*S*': 0.00; 'interpreter': 0.05; 'subject:while': 0.07; 'def': 0.13; '"while': 0.16; 'boolean': 0.16; 'maybe': 0.21; "doesn't": 0.22; 'wrong?': 0.23; 'code': 0.25; 'tried': 0.27; 'looks': 0.27; 'pass': 0.28; 'true,': 0.28; 'anyone': 0.31; 'that,': 0.32; 'message-id:@gmail.com': 0.33; 'header:User-Agent:1': 0.33; 'instead': 0.33; 'to:addr:python- list': 0.33; 'there': 0.33; 'received:209.85.212': 0.34; '(to': 0.36; 'but': 0.37; 'received:google.com': 0.37; 'using': 0.37; 'received:209.85': 0.38; 'think': 0.38; 'received:192': 0.38; 'sometimes': 0.38; 'why': 0.39; 'received:192.168.1': 0.39; 'received:209': 0.39; 'to:addr:python.org': 0.40; 'difference': 0.40; 'more': 0.61; '13)': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject :content-type:content-transfer-encoding; bh=WT8nEvwVBj1SNILuvKCXjuMq/uJaFWXbcA6aRlJAjy8=; b=DSDAgOFzfnzvmRWsftFW9dgFhSvi6YaGKPc1MLrQpl3cVo6KZS3mBgcQnH83i8a4tM 33O/76CfEuOOPXjCSXdh+RYq+7sfkjRZH0dgkIveelZnybLFq2o7Z8KJTHy0kjLbk6+D Of2oQ77h3ws99rJEx7z7/rkWh9fQWDLYFqwEE=
Date Sat, 21 Jan 2012 13:47:00 +0000
From Andrea Crotti <andrea.crotti.0@gmail.com>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:9.0) Gecko/20111224 Thunderbird/9.0.1
MIME-Version 1.0
To python-list@python.org
Subject while True or while 1
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
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.4907.1327153625.27778.python-list@python.org> (permalink)
Lines 36
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1327153625 news.xs4all.nl 6921 [2001:888:2000:d::a6]:55121
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:19188

Show key headers only | View raw


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

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


Thread

while True or while 1 Andrea Crotti <andrea.crotti.0@gmail.com> - 2012-01-21 13:47 +0000
  Re: while True or while 1 Erik Max Francis <max@alcyone.com> - 2012-01-21 13:14 -0800

csiph-web