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.