I'm currently reading chapter 5.8 of Dive Into Python and Mark Pilgrim says:
There are no constants in Python. Everything can be changed if you try hard enough. This fits with one of the core principles of Python: bad behavior should be discouraged but not banned. If you really want to change the value of None, you can do it, but don't come running to me when your code is impossible to debug.
I tried this in the interpreter
None = "bad"
I get a SyntaxError: assignment to None
Just out o开发者_C百科f curiosity how do you change None?
EDIT:
Interestingly:
>>> import __builtin__
>>> __builtin__.None is None
True
>>> None
>>> None = "bad"
File "<stdin>", line 1
SyntaxError: assignment to None
>>> __builtin__.None = "bad"
File "<stdin>", line 1
SyntaxError: assignment to None
>>> setattr(__builtin__, "None", "bad")
>>> __builtin__.None
'bad'
>>> None
>>> __builtin__.None is None
False
>>> __builtin__.None = None
File "<stdin>", line 1
SyntaxError: assignment to None
Also
>>> class Abc:
... def __init__(self):
... self.None = None
...
File "<stdin>", line 3
SyntaxError: assignment to None
>>> class Abc:
... def __init__(self):
... setattr(self,'None',None)
...
>>>
So I guess 'None = ' just doesn't work in any context
You first have to install an old version of Python (I think it needs to be 2.2 or older). In 2.4 and newer for certain (and I believe in 2.3) the assignment in question is a syntax error. Mark's excellent book is, alas, a bit dated by now.
精彩评论