Why do I get AttributeError: 'NoneType' object has no attribute 'something'?

前端 未结 10 2235
长情又很酷
长情又很酷 2020-11-21 13:36

I keep getting an error that says

AttributeError: \'NoneType\' object has no attribute \'something\'
10条回答
  •  后悔当初
    2020-11-21 13:53

    Others have explained what NoneType is and a common way of ending up with it (i.e., failure to return a value from a function).

    Another common reason you have None where you don't expect it is assignment of an in-place operation on a mutable object. For example:

    mylist = mylist.sort()
    

    The sort() method of a list sorts the list in-place, that is, mylist is modified. But the actual return value of the method is None and not the list sorted. So you've just assigned None to mylist. If you next try to do, say, mylist.append(1) Python will give you this error.

提交回复
热议问题