简体   繁体   English

如何在Python中更改None的值?

[英]How to change the value of None in Python?

I'm currently reading chapter 5.8 of Dive Into Python and Mark Pilgrim says: 我目前正在阅读Dive Into Python的第5.8章,Mark Pilgrim说:

There are no constants in Python. 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. 这符合Python的核心原则之一:不应该阻止不良行为但不禁止行为。 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. 如果你真的想要更改None的值,你可以这样做,但是当你的代码无法调试时,不要来找我。

I tried this in the interpreter 我在翻译中试过这个

None = "bad"

I get a SyntaxError: assignment to None 我得到一个SyntaxError:赋值给None

Just out of 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). 首先必须安装版本的Python(我认为它需要2.2或更早版本)。 In 2.4 and newer for certain (and I believe in 2.3) the assignment in question is a syntax error. 在2.4及更新版本(我相信2.3)中,有问题的分配是语​​法错误。 Mark's excellent book is, alas, a bit dated by now. 马克的优秀书籍,唉,现在有点过时了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM