gogoWebsite

[report an error]deep analysisAttributeError: ‘NoneType‘ object has no attribute ‘xxx‘(hold a whip-round)

Updated to 4 months ago

The reason there is a related blog to also write about it is because the following blogs speak to their respective focuses. Therefore, I will be digging deeper into the cause and the several issues that lead to that cause! If the explanation is clear, please give me a like ☀️

Refer to the blog post:

  1. 《AttributeError: ‘NoneType’ object has no attribute……error analysis》
  2. 《【error analysis】AttributeError: ‘NoneType’ object has no attribute ‘xxx’》
  3. 《success storyAttributeError: ‘NoneType’ object has no attribute ‘find_all’》
  • Translation:Error attribute: The "nonetype" object does not have the 'xxx' attribute.

One,Nonetypetypology

See blog"None Type
The gist:

  1. NoneType typeThere is only one valueNone.
    在这里插入图片描述
  2. Nonetypetype does not support any arithmetic and has no built-in methods

II. Reasons for reporting errors

aforementionedNonetypeThere is only one typeNone, the inverse inference is also that the statement in which the problem occurs contains theNoneThis variable. And as we know from earlier, theNoneis without any built-in methods, and thus without any properties.

  • Location: This is the error in my code at line 14
    在这里插入图片描述
  • Test: Replace the variable model with None.
    在这里插入图片描述
    You can see the reason for the reported error, which can be said to be identical!
  • Verification: I willmodelThe type of the printout , the
    在这里插入图片描述在这里插入图片描述
  • Conclusion: For unknown reasons.modelThis variable becomesNone

III. Find the variable forNoneunderlying causes

variable isNoneThe reason for this often lies in the...

3.1 Unsuccessful reading of data from a file ~~ Incorrect file path

Currently, the only reason I can think of for unsuccessful reads is incorrect file paths. Here are some examples and how to verify! You can also see《FileNotFoundError: [Errno 2] No such file or directory》

  1. file path in theFolder nameincorrect
  2. in the filename pathSpecial symbols in Chinese and Englishincorrect
  3. filenameformat suffixErrors such as.png–>,jpg
  • Test Methods:
    (filepath)

Relative paths in use...

  1. ./cap (a poem)../misuse
  • Test Method.
    ()Get the current python working path

Get the absolute path of ...
Act I:
在这里插入图片描述
Law II:
在这里插入图片描述
Reminder: two methods inFile path and filename splicingIt's time to add\

3.2 Functions returning None values

In some customized functions, there will be a case where it returns None. As follows, I have customized a function.

def fun(x):
    if isinstance(x,str) :
        return x 
    else:
        return  None

Meaning: if xs is a string type, then return x; if not, then return None value.