python – What is a NoneType object?
python – 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 dont 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, its returned by re.search
when the regex doesnt 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 didnt provide a command-line option and optparse
gave you None
for that options 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.
For the sake of defensive programming, objects should be checked against nullity before using.
if obj is None:
or
if obj is not None:
python – What is a NoneType object?
NoneType
is simply the type of the None
singleton:
>>> type(None)
<type NoneType>
From the latter link above:
None
The sole value of the type
NoneType
.None
is frequently used to represent the absence of a value, as when default arguments are not passed to a function. Assignments toNone
are illegal and raise aSyntaxError
.
In your case, it looks like one of the items you are trying to concatenate is None
, hence your error.