A look at the best new features in Python 3.9

栏目: IT技术 · 发布时间: 5年前

内容简介:That is everything we can look forward to with the upcoming Python 3.9. If you really can’t wait, the most recent beta release — 3.9.0b3 —This

It’s that time again: a new version of Python is imminent. Now in beta (3.9.0b3), we will soon be seeing the full release of Python 3.9.

Some of the newest features are incredibly exciting, and it will be amazing to see them used after release. We’ll cover the following ones:

  • Dictionary Union Operators
  • Type Hinting
  • Two New String Methods
  • New Python Parser

Let’s take a first look at these new features and how to use them.

Dictionary unions

One of my favorite new features with a sleek syntax. If we have two dictionaries a   and   b   that we need to merge, we can now use the   union operators .

We have the merge   operator:

A look at the best new features in Python 3.9

And the update operator |= , which updates the original dictionary:

A look at the best new features in Python 3.9

If our dictionaries share a common key , the key-value pair in the second dictionary will be used:

A look at the best new features in Python 3.9

Dictionary Update with Iterables

Another cool behavior of the |= operator is the ability to update the dictionary with new key-value pairs using an iterable object — like a list or generator:

A look at the best new features in Python 3.9

If we attempt the same with the standard union operator |   we will get a   TypeError   as it will only allow unions between   dict   types.

A look at the best new features in Python 3.9

Type hinting

Python is dynamically typed, meaning we don’t need to specify datatypes in our code .

This is okay, but sometimes it can be confusing, and suddenly Python’s flexibility becomes more of a nuisance than anything else.

Since 3.5, we could specify types, but it was pretty cumbersome. This update has truly changed that, let’s use an example:

A look at the best new features in Python 3.9
No type hinting (left) v type hinting with 3.9 (right)

In our add_int  function , we clearly want to add the same number to itself (for some mysterious undefined reason). But our editor doesn’t know that, and it is perfectly okay to add two strings together using   +   — so no warning is given.

What we can now do is specify the expected input type as int . Using this, our editor picks up on the problem immediately.

We can get pretty specific about the types included too, for example:

A look at the best new features in Python 3.9

Type hinting can be used everywhere — and thanks to the new syntax, it now looks much cleaner:

A look at the best new features in Python 3.9
We specify sum_dict’s argument as a dict and the returned value as an int. During the test definition, we also determine its type.

String methods

Not as glamourous as the other new features, but it is still worth a mention as it is particularly useful. Two new string methods for removing prefixes and suffixes have been added:

A look at the best new features in Python 3.9

New parser

This one is more of an out-of-sight change but has the potential of being one of the most significant changes for the future evolution of Python.

Python currently uses a predominantly LL(1)-based grammar, which in turn can be parsed by a LL(1) parser — which parses code top-down, left-to-right, with a lookahead of just one token.

Now, I have almost no idea of how this works — but I can give you a few of the current issues in Python due to the use of this method:

  • Python contains non-LL(1) grammar; because of this, some parts of the current grammar use workarounds, creating unnecessary complexity.
  • LL(1) creates limitations in the Python syntax (without possible workarounds).   This issue   highlights that the following code simply cannot be implemented using the current parser (raising a   SyntaxError ):
A look at the best new features in Python 3.9
  • LL(1) breaks with left-recursion in the parser. Meaning particular recursive syntax can cause an infinite loop in the parse tree.   Guido van Rossum , the creator of Python,   explains this here .

All of these factors (and many more that I simply cannot comprehend) have one major impact on Python; they limit the evolution of the language.

The new parser, based on PEG,  will allow the Python developers to be significantly more flexible — something we will begin to notice from   Python 3.10 onwards .

That is everything we can look forward to with the upcoming Python 3.9. If you really can’t wait, the most recent beta release — 3.9.0b3 — is available here .

This article was originally published on Towards Data Science by James Briggs , an AI Consultant based in London. He is fascinated by the phenomenal advances that are being made within the tech eco-system daily and loves writing about AI, Python, and programming in general. Follow him on Twitter .


以上所述就是小编给大家介绍的《A look at the best new features in Python 3.9》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

长尾理论2.0

长尾理论2.0

安德森 / 乔江涛、石晓燕 / 中信出版社 / 2009-5 / 42.00元

《长尾理论2.0》是克里斯·安德森对所有问题最明确的回答。在此书中,他详细阐释了长尾的精华所在,揭示了长尾现象是如何从工业资本主义原动力——规模经济与范围经济——的矛盾中产生出来的。长尾现象虽然是明显的互联网现象,但其商务逻辑本身,却是从工业经济中自然而然“长”出来的,网络只是把酝酿了几十年的供应链革命的诸多要素简单地结合在一起了。同时,长尾理论转化为行动,最有力、最可操作的就是营销长尾,通过口碑......一起来看看 《长尾理论2.0》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

随机密码生成器
随机密码生成器

多种字符组合密码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器