On Tuesday 24 July 2007 13:11, Kent Johnson wrote: > 'while 1' is optimized to just a jump - the compiler recognizes that the > test is not needed and skips it. 'while True' requires looking up the > value of the name 'True' and testing it.
This may seem counter intuitive, but the reason for this (for anyone puzzled) is because True is a name for the value representing boolean true, rather than a keyword for the value. If it were a keyword for the value, then like "1" it could not change in value. However since it isn't, it can. For example, True can become false as the following demonstrates, so you do have to do those steps of looking up the value of the name "True" and test it: Python 2.5 (r25:51908, Nov 27 2006, 19:14:46) [GCC 4.1.2 20061115 (prerelease) (SUSE Linux)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> True = 10 >>> while True: ... print True ... True = True -1 ... 10 9 8 7 6 5 4 3 2 1 >>> Rebinding True to anything else is a bad idea, but doable :-) If you change True to "True" you get the same behaviour - python detects an immutable object in the condition and optimises it: >>> dis.dis(compile('while "True": pass', '', 'exec')) 1 0 SETUP_LOOP 3 (to 6) >> 3 JUMP_ABSOLUTE 3 >> 6 LOAD_CONST 0 (None) 9 RETURN_VALUE Regards, Michael _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor