Python 3.9 is here! Volunteers from all over the world have been working on improvements to Python for the past year. While beta versions have been available for some time, the first official version of Python 3.9 was released on October 5, 2020.
Every release of Python includes new, improved, and deprecated features, and Python 3.9 is no different. The documentation gives a complete list of the changes. Below, you’ll take an in-depth look at the coolest features that the latest version of Python brings to the table.
In this tutorial, you’ll learn about:
- Accessing and calculating with time zones
- Merging and updating dictionaries effectively
- Using decorators based on expressions
- Combining type hints and other annotations
To try out the new features yourself, you need to have Python 3.9 installed. You can download and install it from the Python home page. Alternatively, you can try it out using the official Docker image. See Run Python Versions in Docker: How to Try the Latest Python Release for more details.
Free Bonus: Click here to get access to a free "The Power of Python Decorators" guide that shows you 3 advanced decorator patterns and techniques you can use to write to cleaner and more Pythonic programs.
Proper Time Zone Support#
Python has extensive support for working with dates and times through the datetime
module in the standard library. However, support for working with time zones has been somewhat lacking. Until now, the recommended way of working with time zones has been to use third-party libraries like dateutil
.
The biggest challenge to working with time zones in plain Python has been that you’ve had to implement time zones rules yourself. A datetime
supports setting time zones, but only UTC is immediately available. Other time zones need to be implemented on top of the abstract tzinfo
base class.
Accessing Time Zones#
You can get a UTC time stamp from the datetime
library like this:
>>> from datetime import datetime, timezone
>>> datetime.now(tz=timezone.utc)
datetime.datetime(2020, 9, 8, 15, 4, 15, 361413, tzinfo=datetime.timezone.utc)
Note that the resulting time stamp is time zone aware. It has an attached time zone as specified by tzinfo
. Time stamps without any time zone information are called naive.
Paul Ganssle has been the maintainer of dateutil
for years. He joined the Python core developers in 2019 and helped add a new zoneinfo
standard library that makes working with time zones much more convenient.
zoneinfo
provides access to the Internet Assigned Numbers Authority (IANA) Time Zone Database. The IANA updates its database several times each year, and it’s the most authoritative source for time zone information.
Using zoneinfo
, you can get an object describing any time zone in the database:
>>> from zoneinfo import ZoneInfo
>>> ZoneInfo("America/Vancouver")
zoneinfo.ZoneInfo(key='America/Vancouver')
You access a time zone using one of several keys. In this case, you use "America/Vancouver"
.
Note: zoneinfo
uses an IANA time zone database residing on your local computer. It’s possible—on Windows in particular —that you don’t have any such database or that zoneinfo
won’t be able to locate it. If you get an error like the following, then zoneinfo
hasn’t been able to locate a time zone database:
>>> from zoneinfo import ZoneInfo
>>> ZoneInfo("America/Vancouver")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZoneInfoNotFoundError: 'No time zone found with key America/Vancouver'
A Python implementation of the IANA Time Zone Database is available on PyPI as tzdata
. You can install it with pip
:
$ python -m pip install tzdata
Once tzdata
is installed, zoneinfo
should be able to read information about all supported time zones. tzdata
is maintained by the Python core team. Note that you need to keep the package updated in order to have access to the latest changes in the IANA Time Zone Database.
You can make time zone–aware time stamps using the tz
or tzinfo
arguments to datetime
functions:
>>> from datetime import datetime
>>> from zoneinfo import ZoneInfo
>>> datetime.now(tz=ZoneInfo("Europe/Oslo"))
datetime.datetime(2020, 9, 8, 17, 12, 0, 939001,
tzinfo=zoneinfo.ZoneInfo(key='Europe/Oslo'))
>>> datetime(2020, 10, 5, 3, 9, tzinfo=ZoneInfo("America/Vancouver"))
datetime.datetime(2020, 10, 5, 3, 9,
tzinfo=zoneinfo.ZoneInfo(key='America/Vancouver'))
Having the time zone recorded with the time stamp is great for record keeping. It also makes it convenient to convert between time zones:
>>> from datetime import datetime
>>> from zoneinfo import ZoneInfo
>>> release = datetime(2020, 10, 5, 3, 9, tzinfo=ZoneInfo("America/Vancouver"))
>>> release.astimezone(ZoneInfo("Europe/Oslo"))
datetime.datetime(2020, 10, 5, 12, 9,
tzinfo=zoneinfo.ZoneInfo(key='Europe/Oslo'))
Note that the time in Oslo is nine hours later than in Vancouver.
Investigating Time Zones#
Read the full article at https://realpython.com/python39-new-features/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]
from Planet Python
via read more
No comments:
Post a Comment