On 12/12/2010 10:30 AM, Christian Heimes wrote: > Am 12.12.2010 15:14, schrieb Max Countryman: >> I'm sure this has been brought up many times, but a quick Googling didn't >> yield the decisive results I was hoping for, so I apologize if this has >> already been addressed in great detail somewhere else. >> >> I am wondering what the rationale is behind preferring while True over while >> 1? For me, it seems that using True provides more clarity, but is that the >> only benefit? Is while 1 more prone to errors? > > In Python 2.x, "while 1" is slightly faster than "while True". The > interpreter can't optimize "while True" because the name "True" can be > bind to another value. In Python 3.x it's no longer possible to rebind > the names True and False just like None in Python 2.x > Would you care to quantify how much CPU time that optimization will typically save for a loop of fair magnitude (say, a billion iterations)?
Python is designed to provide readable code. Writing while True: ... is much more legible than its pre-True couterpart while 1: ... and is, I'd say, therefore to be preferred (except in a code base intended to compile on 2.2 and before). regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon 2011 Atlanta March 9-17 http://us.pycon.org/ See Python Video! http://python.mirocommunity.org/ Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list