What is a 'NoneType' object?

前端 未结 9 1326
南笙
南笙 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:36

    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
    
    0 讨论(0)
  • 2020-11-28 03:39

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

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

    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.

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

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

    if obj is None:
    

    or

    if obj is not None:
    
    0 讨论(0)
  • 2020-11-28 03:47

    A nonetype is the type of a None.

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

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

    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.

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