Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #19188 > unrolled thread
| Started by | Andrea Crotti <andrea.crotti.0@gmail.com> |
|---|---|
| First post | 2012-01-21 13:47 +0000 |
| Last post | 2012-01-21 13:14 -0800 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
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
| From | Andrea Crotti <andrea.crotti.0@gmail.com> |
|---|---|
| Date | 2012-01-21 13:47 +0000 |
| Subject | while True or while 1 |
| Message-ID | <mailman.4907.1327153625.27778.python-list@python.org> |
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
[toc] | [next] | [standalone]
| From | Erik Max Francis <max@alcyone.com> |
|---|---|
| Date | 2012-01-21 13:14 -0800 |
| Message-ID | <pL-dnUvq2eEtt4bSnZ2dnUVZ5vsAAAAA@giganews.com> |
| In reply to | #19188 |
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.
No (with the exception of `True` and `False` being rebinable in Python
2). The idiomatic `while 1` notation comes from back in the pre-Boolean
days. In any reasonably modern implementation, `while True` is more
self-documenting. I would imagine the primary reason people still do
it, any after-the-fact rationalizations aside, is simply habit.
--
Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Jabber erikmaxfrancis
Ambition can creep as well as soar.
-- Edmund Burke
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web