Python 什么是“NoneType”对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21095654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a 'NoneType' object?
提问by insecure-IT
I'm getting this error when I run my python script:
运行 python 脚本时出现此错误:
TypeError: cannot concatenate 'str' and 'NoneType' objects
I'm pretty sure the 'str' means string, but I dont know what a 'NoneType' object is. My script craps out on the second line, I know the first one works because the commands from that line are in my asa as I would expect. At first I thought it may be because I'm using variables and user input inside send_command.
我很确定“str”表示字符串,但我不知道“NoneType”对象是什么。我的脚本在第二行失败了,我知道第一个行得通,因为该行中的命令正如我所期望的那样在我的 asa 中。起初我认为这可能是因为我在 send_command 中使用了变量和用户输入。
Everything in 'CAPS' are variables, everything in 'lower case' is input from 'parser.add_option' options.
'CAPS' 中的所有内容都是变量,'小写' 中的所有内容都是从 'parser.add_option' 选项输入的。
I'm using pexpect, and optparse
我正在使用 pexpect 和 optparse
send_command(child, SNMPGROUPCMD + group + V3PRIVCMD)
send_command(child, SNMPSRVUSRCMD + snmpuser + group + V3AUTHCMD + snmphmac + snmpauth + PRIVCMD + snmpencrypt + snmppriv)
采纳答案by Burhan Khalid
NoneTypeis the type for the Noneobject, which is an object that indicates no value. Noneis 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.searchwhen the regex doesn't match, or dict.getwhen the key has no entry in the dict. You cannot add Noneto strings or other objects.
NoneType是None对象的类型,它是一个表示没有值的对象。None是“不返回任何内容”的函数的返回值。对于搜索某些东西并且可能会或可能不会找到它的函数来说,它也是一个常见的默认返回值;例如,re.search当正则表达式不匹配时或dict.get当键在字典中没有条目时返回。您不能添加None到字符串或其他对象。
One of your variables is None, not a string. Maybe you forgot to returnin one of your functions, or maybe the user didn't provide a command-line option and optparsegave you Nonefor that option's value. When you try to add Noneto a string, you get that exception:
您的变量之一是None,而不是字符串。也许你忘记了你的return一个函数,或者用户没有提供命令行选项,optparse而是None为你提供了该选项的值。当您尝试添加None到字符串时,您会收到该异常:
send_command(child, SNMPGROUPCMD + group + V3PRIVCMD)
One of groupor SNMPGROUPCMDor V3PRIVCMDhas Noneas its value.
其中之一grouporSNMPGROUPCMD或V3PRIVCMDhasNone作为其值。
回答by André Laszlo
It means you're trying to concatenate a string with something that is None.
这意味着您正在尝试将字符串与None.
None is the "null" of Python, and NoneTypeis its type.
None 是 Python 的“空值”,NoneType也是它的类型。
This code will raise the same kind of error:
此代码将引发相同类型的错误:
>>> bar = "something"
>>> foo = None
>>> print foo + bar
TypeError: cannot concatenate 'str' and 'NoneType' objects
回答by arshajii
NoneTypeis simply the type of the Nonesingleton:
>>> type(None)
<type 'NoneType'>
From the latter link above:
从上面的后一个链接:
NoneThe sole value of the type
NoneType.Noneis frequently used to represent the absence of a value, as when default arguments are not passed to a function. Assignments toNoneare illegal and raise aSyntaxError.
None类型的唯一值
NoneType。None经常用于表示没有值,就像默认参数没有传递给函数一样。分配给None是非法的并引发SyntaxError.
In your case, it looks like one of the items you are trying to concatenate is None, hence your error.
在您的情况下,您尝试连接的项目之一似乎是None,因此您的错误。
回答by santosh singh
In Python, to represent the absence of a value, you can use the Nonevalue types.NoneType.None
在 Python 中,要表示没有值,可以使用None值types.NoneType.None
回答by Dave
Your error's occurring due to something like this:>>> None + "hello world">>>
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
您的错误是由于以下原因导致的:>>> >>> None + "hello world"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
Python's Noneobject is roughly equivalent to null, nil, etc. in other languages.
Python 的None对象大致相当于其他语言中的null、nil等。
回答by Feuermurmel
In the error message, instead of telling you that you can't concatenate two objects by showing their values (a string and Nonein this example), the Python interpreter tells you this by showing the types of the objects that you tried to concatenate. The type of every string is strwhile the type of the single Noneinstance is called NoneType.
在错误消息中,NonePython 解释器不会通过显示它们的值(在本例中是字符串)来告诉您不能连接两个对象,而是通过显示您尝试连接的对象的类型来告诉您这一点。每个字符串str的类型None是调用单个实例的类型NoneType。
You normally do not need to concern yourself with NoneType, but in this example it is necessary to know that type(None) == NoneType.
你通常不需要关心自己NoneType,但在这个例子中,有必要知道这一点type(None) == NoneType。
回答by Gürol Canbek
For the sake of defensive programming, objects should be checked against nullity before using.
为了防御性编程,在使用对象之前应该检查对象是否为空。
if obj is None:
or
或者
if obj is not None:
回答by SeanOTRS
One of the variables has not been given any value, thus it is a NoneType. You'll have to look into why this is, it's probably a simple logic error on your part.
其中一个变量没有被赋予任何值,因此它是一个 NoneType。您必须研究为什么会这样,这可能是您的一个简单的逻辑错误。
回答by Chogg
A nonetype is the type of a None.
nonetype 是 None 的类型。
See the docs here: https://docs.python.org/2/library/types.html#types.NoneType
请参阅此处的文档:https: //docs.python.org/2/library/types.html#types.NoneType

