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:
- 《AttributeError: ‘NoneType’ object has no attribute……error analysis》
- 《【error analysis】AttributeError: ‘NoneType’ object has no attribute ‘xxx’》
- 《success storyAttributeError: ‘NoneType’ object has no attribute ‘find_all’》
- Translation:
Error attribute: The "nonetype" object does not have the 'xxx' attribute.
One,Nonetype
typology
See blog"None Type
The gist:
- NoneType typeThere is only one value
None
. -
Nonetype
type does not support any arithmetic and has no built-in methods
II. Reasons for reporting errors
aforementionedNonetype
There is only one typeNone
, the inverse inference is also that the statement in which the problem occurs contains theNone
This variable. And as we know from earlier, theNone
is 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 will
model
The type of the printout , the - Conclusion: For unknown reasons.
model
This variable becomesNone
III. Find the variable forNone
underlying causes
variable isNone
The 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》
- file path in theFolder nameincorrect
- in the filename pathSpecial symbols in Chinese and Englishincorrect
- filenameformat suffixErrors such as
.png
–>,jpg
- Test Methods:
(filepath)
Relative paths in use...
-
./
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.