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

前端 未结 10 2009
长情又很酷
长情又很酷 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

    Consider the code below.

    def return_something(someint):
     if  someint > 5:
        return someint
    
    y = return_something(2)
    y.real()
    

    This is going to give you the error

    AttributeError: 'NoneType' object has no attribute 'real'

    So points are as below.

    1. In the code, a function or class method is not returning anything or returning the None
    2. Then you try to access an attribute of that returned object(which is None), causing the error message.

提交回复
热议问题