127

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)
dreftymac
  • 31,404
  • 26
  • 119
  • 182
insecure-IT
  • 2,068
  • 4
  • 18
  • 26
  • 2
    We need to see more code and the input - NoneTyoe means that one of the variables has not been set – mmmmmm Jan 13 '14 at 15:59
  • 5
    `NoneType` is the type of the singleton value `None`. One of your values is not a string. – Martijn Pieters Jan 13 '14 at 15:59
  • Quick&Dirty: put each term to concatenate in `str()` str(SNMPSRVUSRCMD) + str(snmpuser) + ... – PeterMmm Jan 13 '14 at 16:01
  • 4
    @PeterMmm That's a terrible idea -- the problem will just manifest itself (perhaps worse) further down the line. It would be better to figure out *why* one of the variables is None to begin with. – arshajii Jan 13 '14 at 16:10

13 Answers13

116

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.

user2357112
  • 260,549
  • 28
  • 431
  • 505
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • 1
    Thanks!! You where correct, one of my user input options was not taking for some reason. I added this as a "name" vs an option and boom, it worked. Now I just have to find out why this is not taking properly as an option, all the other options are working fine. – insecure-IT Jan 13 '14 at 16:26
  • 1
    "Not set" is a bit vague...if a variable is not defined and you try to perform some operation with it, you should get a `NameError`. It could also be set, but equal to `None` ("set to nothing" = "not set"?), then you might get some `TypeError`. – Nick T Apr 19 '16 at 20:14
  • 1
    Basically (for the C/Java folks here) none = null – Zoe Aug 17 '17 at 12:20
35

For the sake of defensive programming, objects should be checked against nullity before using.

if obj is None:

or

if obj is not None:
Gürol Canbek
  • 1,096
  • 1
  • 14
  • 20
  • 4
    "is" should only be used if you need it. The best defensive programming is tests, lots of tests. – Jürgen A. Erhard Feb 18 '17 at 22:52
  • 5
    Defensive programming is one of the most evil things. If you find an error like that, this means you made a mistake programming. Stuff like "checking for null before use" only hides some serious error. You should let the exception be thrown. And don't make programmers throw some custom exception, it is really cumbersome. Let the natural error be thrown. In fact, defensive programming is a symptom that you should be using another language. – bzim Dec 10 '18 at 20:57
  • 7
    I query the database via sqlAlchemy. There's no result. Returned object is None. Which means no result. There is no error. You check if a result is none, if not, you use the result object. Nothing evil about it. The most evil thing about programming is dogmatism. – unity100 Nov 01 '20 at 05:32
29

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 to None are illegal and raise a SyntaxError.

In your case, it looks like one of the items you are trying to concatenate is None, hence your error.

arshajii
  • 127,459
  • 24
  • 238
  • 287
  • Except that the name `NoneType` exists only in [some versions of Python](https://stackoverflow.com/a/68283326/2810305). – Lutz Prechelt Oct 20 '22 at 09:26
  • @LutzPrechelt would the versions of python that don't contain NoneType give the error: TypeError: cannot concatenate 'str' and 'NoneType' objects ? – N8tron Feb 09 '23 at 12:48
  • @N8tron: Yes. There the string 'NoneType' is only some wording an error message, not the builtin name of an object the interpreter would (need to) recognize. – Lutz Prechelt Feb 17 '23 at 11:28
18

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
André Laszlo
  • 15,169
  • 3
  • 63
  • 81
11

In Python

  • NoneType is the type of the None object.
  • 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 None over x == None in your code.
  • You will get None in many places in regular Python code as pointed out by the accepted answer.
  • You will also get None in your own code when you use the function result of a function that does not end with return myvalue or the like.

Representation:

  • There is a type NoneType in 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 of NoneType.
    See the documentation of repr and that of magic functions (or "dunder functions" for the double underscores in their names) in general.

In Python 2.7

In Python 3.0 to 3.9

  • NoneType has been removed from module types, presumably because there is only a single value of this type.
  • It effectively exists nevertheless, it only has no built-in name: You can access NoneType by writing type(None).
  • If you want NoneType back, just define NoneType = type(None).

In Python 3.10+

Lutz Prechelt
  • 36,608
  • 11
  • 63
  • 88
4

In Python, to represent the absence of a value, you can use the None value types.NoneType.None

dǝɥɔS ʇoıןןƎ
  • 1,674
  • 5
  • 19
  • 42
santosh singh
  • 27,666
  • 26
  • 83
  • 129
  • 2
    For Python 3, see: https://stackoverflow.com/questions/21706609/where-is-the-nonetype-located-in-python-3-x – kkurian Jan 18 '18 at 17:57
2

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.

Feuermurmel
  • 9,490
  • 10
  • 60
  • 90
  • I have a very similar problem and trying to detect if the object is NoneType, your affirmation doesn't work for me: `>>> type(key) >>> type(key) == NoneType Traceback (most recent call last): File "", line 1, in NameError: name 'NoneType' is not defined` – RuBiCK Dec 25 '15 at 00:22
  • 2
    @RuBiCK `NoneType` is not defined anywhere (that I am aware of). If you want to check whether a value is `None`, just use `key is None` (see http://stackoverflow.com/questions/23086383/how-to-test-nonetype-in-python). Otherwise you could use `type(key) == type(None)` to the same effect. – Feuermurmel Jan 03 '16 at 19:17
1

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.

Dave
  • 1,212
  • 12
  • 8
1

If you're getting type None for an object, make sure you're returning in the method. For example:

class Node:
    # node definition

then,

def some_funct():
    # some code
    node = Node(self, self.head)
    self.head = node

if you do not return anything from some_func(), the return type will be NoneType because it did not return anything.

Instead, if you return the node itself, which is a Node object, it will return the Node-object type.

def some_func(self):
    node = Node(self, self.head)
    self.head = node
    return node
SL4566
  • 132
  • 1
  • 7
0

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.

SeanOTRS
  • 19
  • 3
0

NoneType is the type of None.

See the Python 2 docs here: https://docs.python.org/2/library/types.html#types.NoneType

Lutz Prechelt
  • 36,608
  • 11
  • 63
  • 88
Chogg
  • 389
  • 2
  • 19
0

It's returned when you have for instance print as your last statement in a function instead of return:

def add(a, b):
    print(a+ b)

x = add(5,5)
print(x)
print(type(x))

y = x + 545
print(y)

TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' <class 'NoneType'>

def add(a, b):
    return (a+ b)

x = add(5,5)
print(x)
print(type(x))

10
<class 'int'>
555
Lutz Prechelt
  • 36,608
  • 11
  • 63
  • 88
0

NoneType is type of None. Basically, The NoneType occurs for multiple reasons,

  • Firstly when you have a function and a condition inside (for instance), it will return None if that condition is not met. Ex:- def dummy(x, y): if x > y: return x res = dummy(10, 20) print(res) # Will give None as the condition doesn't meet.

To solve this return the function with 0, I.e return 0, the function will end with 0 instead of None if the condition is not satisfied.

  • Secondly, When you explicitly assign a variable to a built-in method, which doesn't return any value but None. my_list = [1,2,3] my_list = my_list.sort() print(my_list) #None sort() mutate the DS but returns nothing if you print it. Or lis = None re = lis.something()) print(re) # returns attribute error NonType object has no attribute something