Features of Python 3.9

0
1012
Features of Python 3.9

Introduction to Python 3.9

This article explains the new features in python 3.9, as compared to 3.8. Python 3.9 is the last version providing those Python 2 backward compatibility layers, to give more time to Python projects maintainers to organize the removal of the Python 2 support and add support for Python 3.9.

What’s New in Python 3.9

  1. Relaxed grammar restrictions on decorators.
  2. String methods to remove prefixes and suffixes.
  3. Flexible function and variable annotations.
  4. Fast access to module state from methods of C extension types.
  5. CPython now uses a new parser based on PEG.
  6. A number of Python builtins (range, tuple, set, frozenset, list, dict) are
    now sped up using vectorcall.
  7. garbage collection does not block on resurrected objects;
  8. Time Zone Database is now present in the standard library in the
    ‘zoneinfo’ module.
  9. New Features:-
    Dictionary Merge & Update Operators
    Merge (|) and update (|=) operators have been added to the built-in dict class.

x = {“key1”: “dict1 from x”, “key2”: “dict2 from x”}
y = {“key2”: “dict2 from y”, “key3”: “dict3 from y”}
x | y
{‘key1’: ‘dict1 from x’, ‘key2’: ‘dict2 from y’, ‘key3’: ‘dict3 from y’}
y | x
{‘key2’: ‘dict2 from x’, ‘key3’: ‘dict3 from y’, ‘key1’: ‘dict1 from x’}

New String Methods to Remove Prefixes and Suffixes
str.removeprefix(prefix) and str.removesuffix(suffix) have been added to easily remove an unneeded prefix or a suffix from a string. Corresponding bytes, bytearray, and collections.UserString methods have also been added.

Only one line removes the prefix/suffix.
students = [‘Student.A’, ‘Student.B’, ‘Student.C’]
names_only = [ ]
for student in students:
names_only.append(student.removeprefix(‘Student.’))
print(names_only)
[‘A’, ‘B’, ‘C’]

Zoneinfo
The zoneinfo module brings support for the IANA time zone database to the
standard library.
It adds zoneinfo.ZoneInfo, a concrete datetime.tzinfo implementation backed
by the system’s time zone data.
from zoneinfo import ZoneInfo
from datetime import datetime, timedelta

Daylight saving time

dt = datetime(2020, 10, 7, 12,
tzinfo=ZoneInfo(“America/Los_Angeles”))
print(dt)
2020-10-7 12:00:00-07:00
dt.tzname()
‘PDT’

Standard time

dt += timedelta(days=7)
print(dt)
2020-11-07 12:00:00-08:00
print(dt.tzname())
PST

No need for a from typing import list
the_list: list[int] = [1,2,3,4]
print(the_list)
the_list = ‘1234’
print(the_list)
[1, 2, 3, 4]
1234


Type Hinting Generics in Standard Collections
In type annotations you can now use built-in collection types such as list and dict as generic types instead of importing the corresponding capitalized types (e.g. List or Dict) from typing. You should check for Deprecation Warning in your code When Python 2.7 was still supported, a lot of functionality in Python 3 was kept for backward compatibility with Python 2.7. With the end of Python 2 support, these backward compatibility layers have been removed, or will be removed soon. Most of them emitted a DeprecationWarning warning for several years. For example, using collections.Mapping instead of collections.abc.Mapping emits a DeprecationWarning since Python 3.3, released in 2012. Test your application with the -W default command-line option to see Deprecation Warning and PendingDeprecationWarning, even with -W error to treat them as errors. Warnings Filter can be used to ignore warnings from third-party code. Should we Upgrade to Py-PI 3.9 If you wish to try the new version then you’ll need to use Python3.9.
So, you can upgrade.


Keep in mind, that if you have code running in Python 3.8 without any problem, then you might experience some issues while running the same code in Python 3.9. Because the new PEG parser has naturally not been tested as extensively as
the old one and only time will tell us the truth about it. So, everyone can wait for the first maintenance release: Python 3.9.1

Want to learn Data Science and Machine Learning with Placements? Visit our WEBSITE for more information.

LEAVE A REPLY

Please enter your comment!
Please enter your name here