什么是无价值? [英] What is a None value?
问题描述
我一直在研究Python,我读了一章描述无
的值,但遗憾的是这本书在某些方面并不是很清楚。如果我在那里分享,我想我会找到问题的答案。
I have been studying Python, and I read a chapter which describes the None
value, but unfortunately this book isn't very clear at some points. I thought that I would find the answer to my question, if I share it there.
我想知道无
value 是,你用它做什么?
I want to know what the None
value is and what do you use it for?
而且,我还没有得到这本书的这一部分: / p>
And also, I don't get this part of the book:
为变量赋值
无
是将其重置为$的一种方法b $ b原始的空状态。
Assigning a value of
None
to a variable is one way to reset it to its original, empty state.
这是什么意思?
答案很棒,虽然由于我对计算机世界的了解不足(我还没有学过课程,对象等),所以我不理解大多数答案。这句话是什么意思?
The answers were great, although I didn't understand most of answers due to my low knowledge of the computer world (I haven't learned about classes, objects, etc.). What does this sentence mean?
为变量分配
无
的值是一种方法可以将
重置为原来的空状态。
Assigning a value of
None
to a variable is one way to reset it to its original, empty state.
最终:
最后,我通过寻找不同的答案得到了答案。我必须感谢那些把时间用来帮助我的人(特别是Martijn Pieters和DSM),我希望我能选择所有答案作为最佳答案,但选择仅限于一个。所有答案都很棒。
Finally I've got my answer from looking to different answers. I must appreciate all the people who put their times to help me (especially Martijn Pieters and DSM), and I wish that I could choose all answers as the best, but the selection is limited to one. All of the answers were great.
推荐答案
Martijn的答案解释了无
是在Python中,并正确地指出该书具有误导性。由于Python程序员通常不会说
Martijn's answer explains what None
is in Python, and correctly states that the book is misleading. Since Python programmers as a rule would never say
分配值
无
到变量是将其重置为原始空状态
的一种方法。
Assigning a value of
None
to a variable is one way to reset it to its original, empty state.
很难解释Briggs的含义一种有意义的方式,并解释了为什么这里没有人对它感到满意。一个可能有用的类比:
it's hard to explain what Briggs means in a way which makes sense and explains why no one here seems happy with it. One analogy which may help:
在Python中,变量名称就像贴在物体上的贴纸一样。每个贴纸都有一个独特的名字写在上面,它一次只能放在一个物体上,但如果你愿意,你可以在同一个物体上贴上不止一个贴纸。写的时候
In Python, variable names are like stickers put on objects. Every sticker has a unique name written on it, and it can only be on one object at a time, but you could put more than one sticker on the same object, if you wanted to. When you write
F = "fork"
您将标签F放在字符串对象fork
上。如果你那么写
you put the sticker "F" on a string object "fork"
. If you then write
F = None
您将贴纸移动到无
对象。
Briggs是什么要求你想象的是你没有写贴纸F
,已经 F
无
上的贴纸,你所做的就是移动,来自无
到fork
。因此,当您键入 F =无
时,如果我们决定处理无,则将其重置为原始的空状态
含义空状态
。
What Briggs is asking you to imagine is that you didn't write the sticker "F"
, there was already an F
sticker on the None
, and all you did was move it, from None
to "fork"
. So when you type F = None
, you're "reset[ting] it to its original, empty state", if we decided to treat None
as meaning empty state
.
我可以看到他得到了什么,但那是看待它的坏方法。如果你启动Python并输入 print(F)
,你会看到
I can see what he's getting at, but that's a bad way to look at it. If you start Python and type print(F)
, you see
>>> print(F)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'F' is not defined
和 NameError
表示Python无法识别名称 F
,因为没有这样的贴纸。如果Briggs是正确的并且 F =无
将 F
重置为其原始状态,那么它现在应该在那里,我们应该看
and that NameError
means Python doesn't recognize the name F
, because there is no such sticker. If Briggs were right and F = None
resets F
to its original state, then it should be there now, and we should see
>>> print(F)
None
就像我们输入后一样F =无
并将贴纸贴在无
。
like we do after we type F = None
and put the sticker on None
.
这就是所有这一切。实际上,Python附带了一些贴在对象上的贴纸(内置名称),但是其他的你需要用 F =fork
和 A = 2
和 c17 = 3.14
,然后您可以将它们粘贴在其他对象上(例如 F = 10
或 F =无
;它们都是一样的。)
So that's all that's going on. In reality, Python comes with some stickers already attached to objects (built-in names), but others you have to write yourself with lines like F = "fork"
and A = 2
and c17 = 3.14
, and then you can stick them on other objects later (like F = 10
or F = None
; it's all the same.)
Briggs假装您可能想写的所有可能的贴纸已经卡在无
对象中。
Briggs is pretending that all possible stickers you might want to write were already stuck to the None
object.
这篇关于什么是无价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!