什么是 None 值? [英] What is a None value?

查看:41
本文介绍了什么是 None 值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习 Python,我读了一章描述 None 值,但不幸的是这本书在某些方面不是很清楚.我以为我会找到我的问题的答案,如果我在那里分享它.

我想知道None是什么,你用它做什么?

而且,我不明白这本书的这一部分:

<块引用>

None 的值分配给变量是一种将其重置为它的原始空状态.

这是什么意思?

答案很棒,尽管由于我对计算机世界的了解不足(我还没有了解类、对象等),我无法理解大部分答案.这句话是什么意思?

<块引用>

None 的值分配给变量是重置它的一种方法到其原始的空状态.

决赛:

最后我从寻找不同的答案中得到了答案.我必须感谢所有花时间帮助我的人(尤其是 Martijn Pieters 和 DSM),我希望我可以选择所有答案为最佳,但只能选择一个.所有的答案都很棒.

解决方案

Martijn 的回答解释了 Python 中的 None 是什么,并正确地指出这本书具有误导性.因为 Python 程序员通常永远不会说

<块引用>

None 的值分配给变量是一种将其重置为它的原始空状态.

很难以一种有意义的方式解释布里格斯的意思,并解释为什么这里似乎没有人对此感到满意.一个可能有帮助的类比:

在 Python 中,变量名就像贴在对象上的贴纸.每个贴纸上都写有唯一的名称,一次只能贴在一个物体上,但如果您愿意,您可以在同一个物体上放置多个贴纸.当你写

F = "分叉"

你把标签F"放在一个字符串对象fork"上.如果你再写

F = 无

您将贴纸移至 None 对象.

Briggs 要求你想象的是,你没有贴纸"F",有已经一个None 上的 >F 贴纸,你所做的就是移动它,从 None"fork".因此,当您键入 F = None 时,如果我们决定将 None 视为 空状态.

我能看出他在想什么,但这是一种不好的看待方式.如果你启动 Python 并输入 print(F),你会看到

<预><代码>>>>打印(F)回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中NameError: 名称 'F' 未定义

并且 NameError 意味着 Python 无法识别名称 F因为没有这样的标签.如果 Briggs 是对的并且 F = NoneF 重置为其原始状态,那么它现在应该在那里,我们应该看到

<预><代码>>>>打印(F)没有任何

就像我们在输入 F = None 并将贴纸放在 None 上之后所做的那样.

<小时>

这就是正在发生的一切.实际上,Python 附带了一些已经附加到对象上的贴纸(内置名称),但其他一些你必须自己编写,比如 F = "fork"A = 2c17 = 3.14,然后你可以稍后将它们粘贴到其他对象上(例如 F = 10F = None;就这些一样.)

Briggs 假装你可能想写的所有可能的贴纸都已经粘在 None 对象上.

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.

I want to know what the None value is and what do you use it for?

And also, I don't get this part of the book:

Assigning a value of None to a variable is one way to reset it to its original, empty state.

What does that mean?

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.

Final:

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'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.

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:

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"

you put the sticker "F" on a string object "fork". If you then write

F = None

you move the sticker to the None object.

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.

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

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

like we do after we type F = None and put the sticker on None.


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 is pretending that all possible stickers you might want to write were already stuck to the None object.

这篇关于什么是 None 值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆