After watching Python 3 I decided not to update the new features of 10

Posted by ketola on Mon, 03 Jan 2022 18:16:47 +0100

Python3.10 was released on October 3, 2021. It has been one month now. I believe you have heard about its new features, but I decided not to update it. At present, the version I am using is Python 3 8. No discomfort. Here's the reason why I don't update.

First, the relevant database may not be followed up

If you update to the latest version, can the existing code still run normally without modification? Is the first consideration.

Python's third-party libraries are based on the work of open source volunteers. They do very valuable work for free, and it takes time to adapt to the new Python version. In addition, since the upgrade involves many different groups, coordination and release require more time.

Second, many new features have no use value

Check Python 3 10's useless new features (personal point of view, you can leave a message to spray):

1. with can be bracketed

For example:

with (
    CtxManager1() as example1,
    CtxManager2() as example2,
    CtxManager3() as example3,

I can hardly use this point, and I don't know what kind of situation needs to be written like this. If there are multiple contexts, I can write more in series. If example1 and example2 are related, I can nest:

with CtxManager1() as example1:
    with CtxManager2() as example2:

Isn't it clearer to write in this way?

2. match case

Python3.10. Match and case soft keywords are added. What are soft keywords? Although it is a keyword, it can be used as a variable name. Nevertheless, you certainly don't want match to be your variable name, because it won't report an error:

match match: #The last match is the variable name
    case case:

The syntax of match case in 3.10 is as follows:

match subject:
    case <pattern_1>:
    case <pattern_2>:
    case <pattern_3>:
    case _:

It has so many uses that I can't understand some of them. For example, let's start with a simple:

def match_errno(errno):
    match errno:
        case 0:
        case 1:
        case 42:
        case _:

Here's one_ Indicates that no one matches, which is equivalent to a default value, but_ It is originally used to represent a variable that is no longer used in the future. If it is called in this way, although the explanation is reasonable, it always feels strange:

>>> _ = 42
>>> match_errno(_)

A little more complicated:

def command_split(command):
    match command.split():
        case ["make"]:
            print("default make")
        case ["make", cmd]:
            print(f"make command found: {cmd}")
        case ["restart"]:
        case ["rm", *files]:
            print(f"deleting files: {files}")
        case _:
            print("didn't match")

At first glance, I think there is a problem with this code. cmd and files are undefined variables, but they can be used in the case expression. In fact, there is no problem in the actual operation:

command_split("make clean")
command_split("rm a b c")

# default make
# make command found: clean
# restarting
# deleting files: ['a', 'b', 'c']

After matching, the remaining part can become a variable, which can also be used in this way.

Take another look:

def match_capture_subpattern(command):
    match command.split():
        case ["go", ("north" | "south" | "east" | "west") as direction]:
            print(f"going {direction}")

You see, it can also be used in combination with as. Cow force or not?

Finally, let's look at another one:

match point:
    case Point(x, y) if x == y:
        print(f"The point is located on the diagonal Y=X at {x}.")
    case Point(x, y):
        print(f"Point is not on the diagonal.")

if can also be used in the case. Is it awesome?

In addition to increasing the mental burden of programmers, what benefits can these tricks have? I just finished if elif, which is highly readable. Besides, this match does not improve performance, and sometimes it is even slower. Why bother?

Why didn't Python have a match at the beginning? Now it's 2021. Did you forget your original intention by adding a match?

3. New type tips, laugh to death

Python3.9 write as follows:

from typing import Union
a: Union[int, str] = 1

Python3.10 it can be written as follows:

a: str | int = 1

Seriously, even if you change it to |, I'm unlikely to use it. Type prompt is the advantage of learning other people's static language. It prompts the readability and maintainability of the program. Don't pass the wrong variable type when modifying the code.

Moreover, the Python interpreter does not check whether there are errors in the type prompt at all, which is purely self entertainment:

>>> def fun(x :int) -> str:
...     return x
>>> print(fun('asf'))

If a variable can have many types, will it prompt a fart? There are many prompts. It's better not to prompt. It's a dynamic language. Play by yourself, be funny, ha ha.

If I use type prompt, I can use one type at most. If there is more than one type, I won't prompt. Or modify the code yourself. Do you need so many types?

Third, it is not very stable

Python 3.9.0 was released in October 2020. 3.9. 1 was released 2 months later, including a long list of bug fixes. Similarly, python 3 10. X may also have some bug fixes to let it fly for a while.

Python 3.10 has a new syntax: match case, but some formatting tools or ides may not support it. Even with support, you need to upgrade them.

In short, python 3 10.0 is only a month old and is not very stable.

Then you may ask, when will it be stable?

I think 1-2 lower than the latest version is the best choice, such as Python 3 10, you can consider upgrading to Python 3 8 or Python 3 9, because these versions are stable enough at this time.

On the other hand, unless the version you use declares that it does not support security updates, or reports a bug, you can never update. For example, python 3.6 will end its security update in December 2021 and should be upgraded to Python 3.6 7 or later.

Last words

Based on the above three points, I decided not to update Python 3 10. Of course, python 3 10 also has better new features, such as a more friendly error prompt. When your brackets and quotation marks are not closed, you will throw a clearer error.

I like the two sentences of Python's Zen: simplicity is better than complexity, and clarity is better than obscurity. I hope the update of Python can also maintain the original intention of Python Zen.

If you feel fruitful, you are welcome to like, leave messages, watch and pay attention. That's all for today's sharing. Thank you for reading.

Topics: Python