I\'m getting this error when I run my python script:
TypeError: cannot concatenate \'str\' and \'NoneType\' objects
I\'m pretty sure the \'
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, to represent the absence of a value, you can use the None
value types.NoneType.None
In the error message, instead of telling you that you can't concatenate two objects by showing their values (a string and None
in 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 str
while the type of the single None
instance is called NoneType
.
You normally do not need to concern yourself with NoneType
, but in this example it is necessary to know that type(None) == NoneType
.
For the sake of defensive programming, objects should be checked against nullity before using.
if obj is None:
or
if obj is not None:
A nonetype is the type of a None.
See the docs here: https://docs.python.org/2/library/types.html#types.NoneType
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.