while 1 vs while True
Steve Holden
steve at holdenweb.com
Tue Dec 14 11:33:35 EST 2004
Peter Otten wrote:
> Fredrik Lundh wrote:
>
>
>>Steve Holden wrote:
>>
>>
>>>It was unfortunate that so many people chose to use that for
>>>compatibility, when if they'd used the same code that the win32all
>>>extensions did they could have retained backward compatibility even
>>>across a change to constants:
>>>
>>>try:
>>> True
>>>except AttributeError:
>>> True, False = (1==1), (1!=1)
>>
>>that doesn't work, though:
>>
>>$ python2.1 test.py
>>Traceback (most recent call last):
>> File "test.py", line 2, in ?
>> True
>>NameError: name 'True' is not defined
>
>
>
> Fixing the exception type doesn't help if the change is implemented like the
> constancy of None:
>
>
>>>>try:
>
> .... None
> .... except NameError:
> .... None = object()
> ....
> SyntaxError: assignment to None
>
Aargghh, so the knowledge of None's constancy appears to extend into the
syntax analysis. Damn, that means my last posting was wrong as well.
Should have checked this all out in 2.4 before asserting things that
weren't true, I suppose.
Interestingly the same error occurs even when attempting sideways access:
Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import __builtin__
>>> __builtin__.None = "Rhubarb"
SyntaxError: assignment to None
I'm not sure that I actually agree with the classification of this as a
syntax error - there's actually nothing wrong with the syntax at all in
either of these two cases.
> Another workaround seems viable:
>
>
>>>>globals()["None"] = "Evil Nun"
>>>>None
Clearly the behavior of None has been fairly radically altered if you
can't see a None in globals any more:
Python 2.3.4 (#53, Oct 18 2004, 20:35:07) [MSC v.1200 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> globals()["None"] = "An Evil String"
>>> None
'An Evil String'
Of course this is all in line with the intention that programmatic
references to None should always pick up the singleton instance of type
NoneType.
It's also quite interesting that the Python 2.4 documentation actually
says (in section 2.5 of the Python Library reference" that False and
True are constants, when they clearly aren't "as constant as None":
Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import __builtin__
>>> __builtin__.True = 23
>>> __builtin__.True
23
>>> True
23
>>> __builtin__.None = 23
SyntaxError: assignment to None
>>>
regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119
More information about the Python-list
mailing list