What is a None value?
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.
What is the value of None in memory?
None
is singleton object which doesn't provide (almost) none methods and attributes and its only purpose is to signify the fact that there is no value for some specific operation.
As a real object it still needs to have some headers, some reflection information and things like that so it takes the minimum memory occupied by every python object.
The None Value/Code in Automate the Boring Stuff
To add to the comments and be more clear, your print()
function printed 'Hello!'
to the screen and returned None
to the program. Printing and returning are not the same thing--printing is for the user, returning is for the program. The print goes to the screen only and (usually) cannot be further used by the program. The returned value can be stored in a variable, such as spam
, and used further.
The distinction between printing and returning is important enough that the Python standard is that if a function prints something it should not return any value other than None
, and if the function returns a value it should not print anything. This standard is not followed by many other languages (most notoriously C) and is not consistently followed in Python, but this distinction does help the clarity of Python. If you want to study this concept further, do a search on "side effects" in programming.
Python: None value when returning from function
You are missing a return statement in the if block (when the score is greater than 5):
def playerscore():
global teamtotal
score=input("input score?\n")
if int(score)>5:
print("Your attack score must be between 0 and 5")
return playerscore()
else:
return int(score)
Output:
What name do you want for your player?
shovon
input score?
2
2
What name do you want for your player?
sorida
input score?
23
Your attack score must be between 0 and 5
input score?
43
Your attack score must be between 0 and 5
input score?
234
Your attack score must be between 0 and 5
input score?
1
1
What name do you want for your player?
shody
input score?
2
2
shovon 2
sorida 1
shody 2
From official documentation:
In fact, even functions without a return statement do return a value, albeit a rather boring one. This value is called None (it’s a built-in name).
What is a 'NoneType' object?
NoneType
is the type for the None
object, which is an object that indicates no value. None
is the return value of functions that "don't return anything". It is also a common default return value for functions that search for something and may or may not find it; for example, it's returned by re.search
when the regex doesn't match, or dict.get
when the key has no entry in the dict. You cannot add None
to strings or other objects.
One of your variables is None
, not a string. Maybe you forgot to return
in one of your functions, or maybe the user didn't provide a command-line option and optparse
gave you None
for that option's value. When you try to add None
to a string, you get that exception:
send_command(child, SNMPGROUPCMD + group + V3PRIVCMD)
One of group
or SNMPGROUPCMD
or V3PRIVCMD
has None
as its value.
what is difference between [None] and [] in python?
[]
is an empty list
[None]
is a list with one element. That one element is None
is
checks for reference equality. If both objects refer to the same object by reference then is
will return true.
a = []
b = a
a is [] #false
a is b #true
The None value in Python
Let's say you have a program that reads a temperature from a sensor every minute, and stores them in a list. You might end up with a list like this:
[23, 24, 24, 23, 25, 24]
Now let's assume that sometimes the connection to your sensor fails (maybe it's wireless), and you get no reading. What do you put in your list? If you leave it out, you won't have an even 1-minute spacing between values any more. If you store a 'special' number (e.g. 0 or -1 or something) you'll have to know that the particular 'special' value means 'no measurement'. Using the None value for that is pretty handy, because it makes it clear that there is no value there. E.g. something like this:
[23, 24, None, 23, None, 24]
Related Topics
Nested Ssh Using Python Paramiko
Why Does a Class' Body Get Executed at Definition Time
List Running Processes on 64-Bit Windows
How to Use Selenium to Automate Chase Site Login
Pandas New Column from Groupby Averages
Pandas: Replace Substring in String
Understanding Recursion in Python
Is This Bad Programming Practice in Tkinter
How to Rotate the Sprite and Shoot the Bullets Towards the Mouse Position
How to Check If Any Value Is Nan in a Pandas Dataframe
Styling Multi-Line Conditions in 'If' Statements
Python Locale Error: Unsupported Locale Setting
Asynchronous Method Call in Python
How to Serve Multiple Clients Using Just Flask App.Run() as Standalone
How to Install Pil with Pip on MAC Os
Python Giving Filenotfounderror for File Name Returned by Os.Listdir