I\'m getting this error when I run my python script:
TypeError: cannot concatenate \'str\' and \'NoneType\' objects
I\'m pretty sure the \'
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'
Python's None object is roughly equivalent to null, nil, etc. in other languages.
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.
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.