I'm getting this error when I run my python script:
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.
Everything in 'CAPS' are variables, everything in 'lower case' is input from 'parser.add_option' options.
I'm using pexpect, and optparse
send_command(child, SNMPGROUPCMD + group + V3PRIVCMD)
send_command(child, SNMPSRVUSRCMD + snmpuser + group + V3AUTHCMD + snmphmac + snmpauth + PRIVCMD + snmpencrypt + snmppriv)
12 Answers
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.
For the sake of defensive programming, objects should be checked against nullity before using.
if obj is None:
or
if obj is not None:
NoneType is simply the type of the None singleton:
>>> 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.
In your case, it looks like one of the items you are trying to concatenate is None, hence your error.
It means you're trying to concatenate a string with something that is None.
None is the "null" of Python, and NoneType is its type.
This code will raise the same kind of error:
>>> bar = "something"
>>> foo = None
>>> print foo + bar
TypeError: cannot concatenate 'str' and 'NoneType' objects
In Python
- NoneType is the type of the
Noneobject. - There is only one such object. Therefore, "a None object" and "the None object" and "None" are three equivalent ways of saying the same thing.
- Since all Nones are identical and not only equal,
you should prefer
x is Noneoverx == Nonein your code. - You will get
Nonein many places in regular Python code as pointed out by the accepted answer. - You will also get
Nonein your own code when you use the function result of a function that does not end withreturn myvalueor the like.
Representation:
- There is a type
NoneTypein some but not all versions of Python, see below. - When you execute
print(type(None)), you will get<type 'NoneType'>.
This is produced by the__repr__method ofNoneType.
See the documentation ofreprand that of magic functions (or "dunder functions" for the double underscores in their names) in general.