I bought a Python book with a score of 9.6 on Douban, and found an error every 5 pages? !

△Click " Python cat " above to follow, and reply " 1 " to receive the e-book

dbf420c7cbef28e07d336e9d296a1f81.png

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).

a99b1556c4763b74bc4b4b457e52cf80.jpeg

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?

259c82ee3e39a9123bc65a238efe682f.jpeg

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"?

01bf634aa7de3535bb4888974ab55bc9.jpeg

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?

8af88ffe8d7f05fa898e53cfb4ae4ad4.jpeg

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?

36ea7510d6339d43058ec175bf58245d.jpeg

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. delCan't delete the last element of the list?

e25a5f08ae6ec4dfbf1e7033a135f11f.jpeg

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?

51c89379e4c1adfd62a6142669feea24.jpeg

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?

d49813c9bcabc21d68044b090f323165.jpeg

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?

75b27016471a80722617d39a72c3fab5.jpeg

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?

b83bbc76bcc680222ca2be069abf4498.jpeg

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

c2daec28a5b04b3b4ac40ffdd909a2b4.jpeg

In a code example, the author wrote if result == Nonethis kind of code. However, any experienced Python developer knows that this kind of judgment has to be written if result is Noneas ?

Ask in a low voice: Does the author know the difference isbetween and ==?

Outrageous degree: ⭐⭐⭐⭐⭐

11. Does string replacement only replace once by default?

585aed3c3a48e8f80530fed2c63892d7.jpeg

When introducing the string .replace()method, the author said that this method will only replace once by default, and the ndefault 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 nbut 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

338c7fc4622cebf9022afa2239f142b4.jpeg

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

a4c2a220feec1aeb3d2757552bff017c.jpeg
img

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

e563397cbf0e6c65133b70eedb4fbaa9.jpeg

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?

61a4a69d6eaf0489e6b276f30ea27349.jpeg

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?

86fb3a624df82b3cedb9af1ebfde64ca.jpeg

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:

5842f54724727f4b4fcc32133f217eaf.jpeg

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?

26ebd1c96e4bab11d7907d2a7c36fc87.jpeg

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"?

602ea4c858b34836084c654233c6e5d3.jpeg

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

ceec4add5e8cc8072b730fa05a039228.jpeg

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 ballhas 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

6ea38ceadd10307bd8db8b3e3376e986.jpeg

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? sumThis 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

7ba56ba6f056ba7b98bef7306a724cea.jpeg

selfThe name is a de facto standard, but not an unbreakable constraint.

Outrageous degree: ⭐⭐⭐

22. Is it really illegal to access class attributes through instances?

71ca35168753cca5506312e26c37d5ab.jpeg

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

e19c86eee66ec6a89f9bd69130d158d9.jpeg

In demonstrating try ... finallythe statement, the author gives the above code.

There is a very common bug in this code: file = open(...)it should be tryoutside of , not tryinside . Write it like this, when the file cannot be opened normally, finallythe code block inside will report filean 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?

bfe6d9858e441ef78d33061140d1e0f5.jpeg

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?

d929b1245d7ff579365f9ee84ba59de6.jpeg

Another appalling "practical development" suggestion.

Outrageous degree: ⭐⭐⭐⭐

26. To flip a string must be converted to a list?

ea02e010a1c2ce5ee1cc8166efcd7145.jpeg

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

72bd3f834c19874ee0a03ff57fe2e803.jpeg

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

1924ded25be47be16990776b427a5fc3.jpeg

Outrageous degree: ⭐⭐

29. Irregular type judgment

66dfdb3cd89391ae70f4286edd7aae60.jpeg

In Python, type judgment should be used first isinstance(), not type(...) == str.

Outrageous degree: ⭐⭐

30. filter()The explanation for is very strange

9782e98a2e47e658bb6f6f2bb3aa0a49.jpeg

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. NoneIt 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.

dbd751cd396522a53fc2fcbb49adb162.gif

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 !

Guess you like

Origin blog.csdn.net/chinesehuazhou2/article/details/131078650