Python 3.9 Is Released

From LinuxReviews
Jump to navigationJump to search
Python.jpg

The latest version of the Python programming language has a long list of new features and a lot of optimizations. Working with dictionaries, time-zones, strings and a lot more has become easier with new functionality and syntax simplification.

written by 林慧 (Wai Lin) 2020-10-06 - last edited 2020-10-19. © CC BY

Python Kart.jpg
Python is very easy to learn and very popular programming language.

Python 3.9 is the first release since the Python project decided to move to a 17-month release cycle with yearly releases. The new schedule gives major versions like Python 3.9 one and a half years of full support with an additonal three and a half years of security fixes. Work on Python 3.10 begun five months ago and the first alpha preview of that version, scheduled to be released one year from now, was released alongside Python 3.9.

Python 3.9 brings a lot of new features to the table. The Python documentation's What’s New In Python 3.9 is a long one.

Three types that were previously only available in the typing module have been built into Python 3.9 as base types. You can now simply use list, dict and tuple instead of importing them with typing.List, typing.Dict and typing.Typle:

def greet_all(names: list[str]) -> None:
    for name in names:
        print("Hello", name)

The three new types are described in PEP 585.

On the subject of the Python typing module, there's a new typing.Annotated type that can be used to add context-specific metadata and a new include_extras parameter for typing.get_type_hints() to get at that metadata at runtime. This new functionality is described in PEP 593. Multiple type annotations are supported:

Annotated[int, ValueRange(3, 10), ctype("char")]

Note that the order is preserved and it does matter for equality checks:

Annotated[int, ValueRange(3, 10), ctype("char")] != Annotated[
    int, ctype("char"), ValueRange(3, 10)
]

Dictionaries, defined in the Python dict class, have new merge (| and update (|=) operators. These do not replace the existing dict.update and {**d1, **d2} methods for updating dictionaries, Python 3.9 lets you use both.

>>> x = {"key1": "value1 from x", "key2": "value2 from x"}
>>> y = {"key2": "value2 from y", "key3": "value3 from y"}
>>> x | y
{'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'}
>>> y | x
{'key2': 'value2 from x', 'key3': 'value3 from y', 'key1': 'value1 from x'}

The new dictionary operators are described in-depth in PEP 584.

Python Kart 02.jpg
Python isn't really a racer out-of-the-box, but it can be if you use the PyPy just-in-time interpreter.

The grammar requirements for decorations are far less restrictive in Python 3.9 than they were in previous versions. That change will make maintenance of things like PyQt5 a lot easier. It also makes it easier to read and understand the code. It is now possible to use any expression that would fit in a if or while block as a decorator.

Before you had to use:

buttons = [QPushButton(f'Button {i}') for i in range(10)]
button_0 = buttons [0]
@ button_0.clicked.connect

With Python 3.9 you can simplify that to:

buttons = [QPushButton(f'Button {i}') for i in range(10)]
@buttons [0] .clicked.connect

The next relaxed decoration grammar restrictions are described in detail in PEP 614.

Python Kart 03.jpg
Python. It's a programming language.

Time is an illusion. There's only now. Yet it may be very relevant when you're writing Python programs. What time it is depends on where you are and what time-zone you're in. Working with time-zones in Python is a lot easier in Python 3.9. The zoneinfo module has been expanded to include the IANA time zone database as part of the standard base library.

Python 3.9 - Is It Tea-Time-In Los Angeles.jpg
Working with time-zones is easier in Python 3.9.

You can now specify standard time-zone names with ZoneInfo when you need to work with time-zones:

from zoneinfo import ZoneInfo
from datetime import datetime, timedelta
dt = datetime(2020, 10, 31, 12, tzinfo=ZoneInfo("America/Los_Angeles"))
print(dt)

produces:

2020-10-31 12:00:00-07:00

and if you then run dt.tzname() you get, as you would expect, PDT.

Removing text at the start or end of lines just got a lot easier with Python 3.9.

Python-3.9.removesuffix.jpg
Python 3.9 lets you play with strings using removeprefix and removesuffix.

There are new removeprefix and removesuffix methods that are available for str, bytes, bytearray and collections.UserString objects.

s = "WhatEver"
s.removeprefix ("What")

will leave you with Ever.

The new removeprefix and removesuffix methods are described in PEP 616.

Python 3.9 has a new pidof_open(pid, flags=0) method that can be used to do process management without signals or races. The flags argument doesn't actually do anything, it's just there in case this method needs to be expanded in the future. pidof_open wasn't introduced to the Linux kernel until Linux 5.3, so this won't work on older kernels.

We've only scratched the surface of what's new in Python 3.9 to give you an idea. There's a lot more. You can find an even more detailed overview at docs.python.org/3.9/whatsnew/3.9.html. There's also a really long and very detailed changelog at docs.python.org/release/3.9.0/whatsnew/changelog.html.

5.00
(one vote)


avatar

Vu

14 months ago
Score 0++
The python rulers got it all wrong. They must downgrade to python2.7 for a proper programming language. py2 will be here forever. py3 is just a fad that will vanish before soon.
avatar

Anonymous (34fe59b98d)

2 months ago
Score 0
This is really good for me.
Add your comment
LinuxReviews welcomes all comments. If you do not want to be anonymous, register or log in. It is free.