△Click " Python cat " above to follow, and reply " 1 " to receive the e-book
Hello everyone, I am Brother Cat. Share an article today, you can read it as an entertainment article, and you can also learn a lot from it! Some topics in the article, I have written corresponding articles, so I added some notes.
Author: The Fry
Link: https://juejin.cn/post/7240248679515963451
Copyright: Reprinted here with the authorization of the author. If you need to reprint, please contact the original author.
A few days ago, when I was reading on Douban, I saw a new book on Python, and the evaluation was not bad. The title of the book is "From 0 to 1 Python Instant Learning and Use", author Mo Zhenjie, published by People's Posts and Telecommunications Press in March this year, and its score reached an astonishing 9.6 points (82 reviews).
Douban link: https://book.douban.com/subject/36342454/
Although I am not a zero-based Python developer, I usually write a little technical blog for beginners, so I became interested in this book. I want to see what is unique about this book, and I hope I can learn something from the excellent author.
So I ordered a copy of "From 0 to 1" that day. The book arrived the next evening, and after eagerly reading most of it, I realized that something was not quite right. In the first 14 chapters alone, dozens of knowledge or common sense errors have appeared in the book , some of which are even so low-level that it is unimaginable.
I have selected 30 representative error excerpts here, and invite everyone to identify them together.
Excerpts from some mistakes in the book
1. Can Python only use English letters as variable names?
Python fully supports using unicode as variable names (starting from Python 3), but the author says that only variable names can be composed of English letters.
>>> 世界 = 'World'
>>> print(世界)
World
Outrageous degree: ⭐⭐⭐
Cat Brother Note: This is a very common mistake. If the original book is in English, it seems excusable. I once wrote " Wake Up! Python already supports Chinese variable names! ", which are two very classic English books.
2. Are triple quotes really equal to "comments"?
When introducing comments, the author gave a very direct point of view: "Multi-line comments use triple quotation marks".
This statement is not wrong, at least not rigorous enough. As far as I know, mainstream Python projects basically use #
symbols directly as multi-line comments. And triple quotes, as a way to create string literals, can be used as comments, but they have never become popular on a large scale.
Outrageous degree: ⭐⭐
Cat Brother Note: It is quite strange to use triple quotes as a comment, but it is undeniable that Guido, the father of Python, once recommended this way of writing. See "Why does Python use the # sign as a comment? " "
3. Doesn't Python have a switch statement?
A book that claims to be written based on Python 3.11 version categorically says that Python does not have a switch statement, which I don't understand. Are you really not going to mention it match ... case ...
?
Outrageous degree: ⭐⭐⭐
Cat Brother Note: Python does not have a switch statement, why not design it? See Why doesn't Python support switch statements? "
4. Are there no arrays in Python?
Of course, there are arrays in Python (it’s just not commonly used), why don’t you try to implement it import array
?
Official documentation: array — Efficient arrays of numeric values — Python 3.11.3 documentation
Outrageous degree: ⭐⭐
5. del
Can't delete the last element of the list?
When introducing pop()
the difference between the method of the list and the del statement, the author said that it is impossible to delete the last element of the list with del. I really don't understand why he would say that.
Let's not talk about the fact that comparing pop and del is a bit outrageous. Moreover, del animals[-1]
isn't the execution just deleting the last element? Where is the problem?
Outrageous degree: ⭐⭐⭐
6. Does the list have a join method?
When introducing the built-in method of the list, the author listed join()
in it. But .join()
it has always been a built-in method of the string type, not the list. Maybe the author is using Python from another parallel universe?
Outrageous degree: ⭐⭐⭐⭐⭐
7. Does assigning an empty list to a variable clear the list?
When introducing how to clear the list, the author mentioned that assigning an empty list to the original variable can achieve the purpose of "clearing" the list.
Seeing this statement, I am a little dumbfounded. The assignment operation only modifies the binding relationship (binding) of the original variable. How can we talk about clearing the list?
Outrageous degree: ⭐⭐⭐⭐⭐
8. Lists can only be multiplied by positive integers?
No, lists can be multiplied by any integer:
>>> nums = [1, 2, 3]
>>> nums * 0
[]
>>> nums * -1
[]
Outrageous degree: ⭐⭐⭐
9. A tuple is defined by parentheses?
No, parentheses cannot define tuples, commas are the key to defining tuples.
>>> t = 1, 2
>>> type(t)
<class 'tuple'>
Outrageous degree: ⭐⭐⭐⭐⭐
Note from Cat Brother: This is also a typical mistake. It is recommended that you read the chapter about tuples in "Smooth Python".
10. In the code example, the comparison with None is not standardized
In a code example, the author wrote if result == None
this kind of code. However, any experienced Python developer knows that this kind of judgment has to be written if result is None
as ?
Ask in a low voice: Does the author know the difference is
between and ==
?
Outrageous degree: ⭐⭐⭐⭐⭐
11. Does string replacement only replace once by default?
When introducing the string .replace()
method, the author said that this method will only replace once by default, and the n
default value of is 1.
This error is outrageous. Even if you have written a little bit of Python, you should know that .replace()
the default is replaced countless times? n
(In fact, the parameter name is not called n
but called count
) The default value is -1
, not 1
.
replace(old, new, count=-1, /) method of builtins.str instance
Return a copy with all occurrences of substring old replaced by new.
Outrageous degree: ⭐⭐⭐⭐⭐
12. Missing maxsplit parameter when introducing string split() method
Immediately afterwards, when .replace()
introducing .split()
the method, the author seems to have suddenly forgotten .split()
that also supports the "maximum number of splits ( maxsplit
)" parameter. It is strange that the entire introduction does not mention this parameter at all.
Outrageous degree: ⭐⭐⭐⭐
13. The default segmentation strategy of split() is not spaces
When called split()
without any arguments, Python treats not spaces as delimiters, but all whitespace characters including tabs, newlines, etc., as delimiters.
Outrageous degree: ⭐⭐⭐⭐⭐
14. strip() supports multiple characters as arguments instead of one
When introducing the strip() method, the author said that the function of this method is to remove the first and last characters, which is true. But when introducing the details, the author only writes single character char in "Syntax" and "Code Sample".
This is wrong, strip()
multiple characters are supported by default:
>>> s = 'Hello, World'
>>> s.strip('eHd')
'llo, Worl'
Outrageous degree: ⭐⭐⭐⭐⭐
15. Are dictionaries unordered?
I'm pretty sure, at least in the version of Python 3.11 on which the author claims the book is based, that dictionaries are ordered (insertion order preserved) rather than unordered.
Outrageous degree: ⭐⭐⭐⭐⭐
16. Will printing a collection sort it?
When introducing the collection type, the author made a shocking statement: "When we use print() to output a collection, it will be sorted." I want to break my head, and I can't figure out why he wants to say that, Because we all know that collections in Python are unordered?
It wasn't until I carefully read the previous content of this passage that I vaguely guessed why he had this point of view:
It turned out that {1, 2, .., 5}
when he was printing this collection, he happened to see this so-called "sorting" phenomenon, and took this coincidence caused by the hash value as a fixed behavior of Python.
I really don't know what to say.
Outrageous degree: ⭐⭐⭐⭐⭐
17. Who would write num in result.keys()
such code?
When I saw this code, I fell silent. I haven't seen someone write such a judgment statement for a long time.
As we all know, in Python, the more common way to judge whether a key is in the dictionary is as follows:
if num not in result:
...
Outrageous degree: ⭐⭐⭐
18. Are functions divided into two types: "with return value" and "without return value"?
When introducing functions, the author said that functions are divided into two types: "with or without return value". Forgive me for my ignorance, this is the first time I have heard of this classification method, and it is very original.
I don't know what the author thinks about generator functions (coroutines)? What category are you going to put it in?
Outrageous degree: ⭐⭐
Cat Brother Note: To be precise, Python has two types of "write return value" and "not write return value", but there is absolutely no "function without return value", because all its functions have return values! See " Why do Python functions return None by default?" "
19. Introduce the logical fallacy when passing parameters and reporting errors
When introducing function parameter passing, the author wrote an example that would report an error: "parameters with default values come first". Among them, his explanation of the cause of the error is very puzzling.
The interpreter has already stated that this is a syntax error (SyntaxError). This syntax error ball
has been thrown when defining the function, not in ball('red')
the call statement below.
But why does the author talk about reasons such as "the second parameter does not have a value passed in"? Did he really read the error stack information carefully?
Outrageous degree: ⭐⭐⭐⭐⭐
20. Strange logic when introducing sum() function
When introducing the max(), min() and sum() functions, the author mentioned a very "intimate" sentence: max() and min() support this so-called "support for a set of numbers" calling method, max(1, 2, 3)
while sum()
not support.
This is a fact in itself, but the author did not explain the reason in detail, but just threw out an vague "you can try it yourself".
What's so good about this? sum
This kind of call is not supported, is it because the function signature does not support variable parameters? ?
max() function signature:
max(...)
max(iterable, *[, default=obj, key=func]) -> value
max(arg1, arg2, *args, *[, key=func]) -> value
Look at the sum() function signature:
sum(iterable, /, start=0)
Return the sum of a 'start' value (default: 0) plus an iterable of numbers
There is no support for variable parameters at all.
Outrageous degree: ⭐⭐
21. __init__()
The first parameter of the method does not have to be calledself
self
The name is a de facto standard, but not an unbreakable constraint.
Outrageous degree: ⭐⭐⭐
22. Is it really illegal to access class attributes through instances?
The author says that in "practical development" (this word appears many times in this book), it is not recommended to access class attributes through instances, which is not standardized.
Again, excuse me for my ignorance, this is the first time I have heard this statement. Accessing class properties through instances is really common, I don't know why the author said that.
Outrageous degree: ⭐⭐⭐
23. Exception catch code has bug
In demonstrating try ... finally
the statement, the author gives the above code.
There is a very common bug in this code: file = open(...)
it should be try
outside of , not try
inside . Write it like this, when the file cannot be opened normally, finally
the code block inside will report file
an error that the variable is not defined.
Correct way of writing:
file = open(...)
try:
# Do something with file
finally:
file.close()
Outrageous degree: ⭐⭐⭐⭐
24. Should readlines be preferred for reading files?
In the file reading section, the author throws out a point of view: In actual development (oh my god, another so-called "actual development"), it should be used first rather than because the latter can only read one line at a readlines()
time readline()
...
Again, sorry for my ignorance, this is the first time I've heard something like this.
Why should the author turn a blind eye to the de facto standard practice in Python for line in f:
? You know, it also reads only one line at a time.
Outrageous degree: ⭐⭐⭐⭐
25. Use search and match of regular expressions as little as possible?
Another appalling "practical development" suggestion.
Outrageous degree: ⭐⭐⭐⭐
26. To flip a string must be converted to a list?
Please listen to me, don't bother with a list, you can just write it ''.join(reverse(s))
.
Outrageous degree: ⭐⭐
27. About deep copy and shallow copy
The above passage is almost entirely wrong.
Using
{**persion}
this unpacking method, the shallow copy is completed, not the deep copy claimed by the author. If you don’t believe it, just put a list in the dictionary and try it out.When performing a shallow copy, Python does not distinguish between "primitive types" or "reference types" (what a rare Python term!), shallow copies always only copy references (depending on the variability of the value, the effect is different), only deep copies will recursively copy the value
Outrageous degree: ⭐⭐⭐⭐⭐
28. Code syntax error
Outrageous degree: ⭐⭐
29. Irregular type judgment
In Python, type judgment should be used first isinstance()
, not type(...) == str
.
Outrageous degree: ⭐⭐
30. filter()
The explanation for is very strange
filter(None)
I don't know what the author means by writing such a long list and saying that the results did not meet expectations. None
It is the default value of the parameter of the filter, there is no saying that what should [False, None ...]
satisfy the condition, this is not a so-called "special case."
Outrageous degree: ⭐⭐⭐
postscript
After reading the first half of "From 0 to 1", I closed the book and looked at the promotional copy on the cover: "'Hexagon' Python tutorial, satisfying the learning fantasy of beginners!" , I felt a kind of black humor. Suddenly, I felt very distressed about the book purchase—a total of 86.3 yuan, which was enough for me to drink milk tea for a week.
Of course, we are not saying that there is no gain at all. I reviewed it and analyzed why I bought this strange book:
High score: Douban score 9.6, ranking high, a lot of comments with text are very real, not like brushing (I am too naive)
I have a good impression of the publishing house: People’s Posts and Telecommunications Publishing House published by Turing Company. I have read many Python books published by them before, and I have a very good impression.
I have bought a lot of bad technical books, but the process of reading "From 0 to 1" this time has greatly refreshed my understanding of "how bad a book really is". I really don’t know how such a book full of errors can be published. Let’s just say that if you find someone who has several years of experience in Python and have developed it once, it won’t be like this?
Suddenly, I thought, since the book can be published in this way, can I also publish the book myself? At least I can do it without telling readers: "join is a built-in method of list", "string replacement is replaced once by default", such fantasy.
The article is quite long, so I have the right to buy a book for everyone. I hope that students who are learning Python will avoid this book. I wish you a happy mood.
The Python cat technical exchange group is open! In the group, there are current employees of domestic first-tier and second-tier factories, as well as students studying in domestic and foreign colleges and universities. There are programming veterans with more than ten years of coding experience, and newcomers who have just started primary and secondary schools. The learning atmosphere is good! Students who want to join the group, please reply to the " communication group " in the official account to get Brother Mao's WeChat (decline the advertising party, if you are the one!)~
Not enough? try them
▲ Python Trends Weekly #1: How to systematically learn Python by yourself?
▲ Rye: An experimental Python package management system
▲ Python project engineering best practice guide
▲ Python official seminar: Is it really feasible to completely remove the GIL?
▲ Advanced Python: How Descriptors Work
▲ Why is there a problem with inheriting Python's built-in types? !
If you found this article helpful
Please generously share and like , thank you !