Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the new method of Python 3.9?

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of what the new method of Python 3.9is, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value, I believe you will have something to gain after reading this Python 3.9new method, let's take a look at it.

New string methods str.removeprefix and str.removesuffix

This will be a favorite feature for fans because it solves some of the clutter in Python's old str approach.

Suppose you want to remove the extension .py from the file name, what would you do? You think of course you can use the str.rstrip function to remove the extension from the right end of the string, which is possible:

> file_string = 'my_test_file.py' > file_name = file_string.rstrip (' .py') > file_name'my_test_file'

Except for this fatal problem. See if you can find the problem with this output:

> file_string = 'make_happy.py' > file_name = file_string.rstrip (' .py') > file_name'make_ha'

Why did it delete some of the file names?

Yes. This is because rstrip (and lstrip and strip) does not accept a "string", but rather a set of characters to delete. Therefore, file_string.rstrip ('.py') does not mean deleting only .py, it also means deleting any of the following characters:., p, and y, which is why our filename with ppy at the end will also be deleted. Because of this approach, it is easy to cause errors and confusion in many other Pythonistas.

This is why Python 3.9 adds two new methods: str.removeprefix and str.removesuffix, which process a given string as a string:

> file_string = 'make_happy.py' > file_name = file_string.removesuffix (' .py') > file_name'make_happy' 's new dict merge and update syntax

Python dictionaries can be merged and updated as follows:

> > alice = {'apples': 1,' bananas': 2} > bob = {'carrots': 3} > merged = {* * alice, * * bob} # Dictionary unpacking added in Python 3.5 > merged {' apples': 1, 'bananas': 2,' carrots': 3} > alice {'apples': 1,' bananas': 2} > > alice.update (bob) # Updates alice in-place with bob's values > > alice {'apples': 1,' bananas': 2 'carrots': 3}

Since merging and updating are very common functions of dictionaries (like collections), this newly added operation simply makes these operations easier. | operators are:

> > alice = {'apples': 1,' bananas': 2} > bob = {'carrots': 3} > merged = alice | bob # Merges the two into a new dictionary > > merged {' apples': 1, 'bananas': 2,' carrots': 3} > alice {'apples': 1,' bananas': 2} > alice | = bob # Updates alice in-place > > alice {'apples': 1,' bananas': 2, 'carrots': 3} > types in the standard collection prompt generics

Personally, I'm a big fan of this feature-if you use type annotations, you'll like it, too. Starting with Python 3.9, you can start using all built-in collection types: list, dict, set, collections.deque, etc., instead of typing.List, typing.Deque, etc., and no more typing imports are required!

# Previously:from collections import defaultdictfrom typing import DefaultDict, List, Set values: List [int] = [] words: Set [str] = set () counts: DefaultDict [int, int] = defaultdict (int) # Starting from Python 3.9:from collections import defaultdict values: list [int] = [] words: set [str] = set () counts: defaultdict [int, int] = defaultdict (int)

Python 3.9 introduces two new modules:

Zoneinfo this brings support for the IANA time zone database to the standard library. This means that Python now has built-in support for daylight saving time, national time changes throughout the year, and so on.

Graphlib it has the implementation of topological sorting algorithm. As a result, you now have built-in methods to solve complex graphics problems, such as sorting dependencies in the build system.

CPython has a new and more powerful parser

CPython now uses a new parser based on PEG rather than a parser based on the old LL1 algorithm. This means that there are some restrictions on which syntax can be added to Python, because the old parser simply cannot read more complex code.

An example is a multiline with statement:

# Previously:with open ('file1.txt') as F1,\ open (' file2.txt') as f2: print (f1.read ()) print (f2.read ())

You usually use parentheses around multi-line statements to get more readable line grouping and avoid trailing\-es at the end of the line.

But the old parsers in Python 3.8do not support this syntax:

# Starting from Python 3.9:with (open ('file1.txt') as F1, open (' file2.txt') as f2,): print (f1.read ()) print (f2.read ())

If PEG is needed in the future, new and improved parser algorithms will be able to handle more complex syntax parsing.

Other supplements

CPython uses an annual release cycle, that is, a new small version of CPython is released each year.

A new way for random.Random.randbytes to generate random bytes.

The _ _ file__ built-in variable is always the current absolute path.

Sys.stderr always buffers rows starting with Python 3.9.

Ast module update:

A new ast.unparse method that converts AST back to code.

Add () the option of indent for indentation in ast.dump, similar to json.dumps.

PEP 614, relaxing the syntax restrictions on decorators.

This is the end of the article on "what is the New method of Python 3.9?" Thank you for reading! I believe you all have a certain understanding of "what is the new method of Python 3.9". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report