What is a 'NoneType' object?

前端 未结 9 1332
南笙
南笙 2020-11-28 03:12

I\'m getting this error when I run my python script:

TypeError: cannot concatenate \'str\' and \'NoneType\' objects

I\'m pretty sure the \'

相关标签:
9条回答
  • 2020-11-28 03:53

    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.

    0 讨论(0)
  • 2020-11-28 03:54

    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.

    0 讨论(0)
  • 2020-11-28 04:00

    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.

    0 讨论(0)
提交回复
热议问题