1.Dictionary Update And Merge Operators
Two new operators, | and |= have been added to the built-in dict class.
The | operator is used to merge dictionaries whereas the |= operator can be used to update the dictionaries.
PEP: 584
For Merge: |
For Update: |=
The key rule to remember is that if there are any key conflicts then the rightmost-value will be kept. It means that the last seen value always wins. This is the current behavior of other dict operations too.
More Details: https://www.python.org/dev/peps/pep-0584
Buy Coder's Merchandise:
https://teespring.com/get-coder-s?pid=328&cid=6277&sid=front
2.New Flexible High Performant PEG-Based Parser
The Python 3.9 version is proposing to replace the current LL(1) based Python parser with a new PEG-based parser which is high-performant and stable.
PEP: 617
More details: https://www.python.org/dev/peps/pep-0617
3. New String Functions To Remove Prefix and Suffix
Two new functions/methods have been added to the str object.
The first function removes the prefix. It is str.removeprefix(prefix).
The second function removes the suffix. It is str.removesuffix(suffix).
PEP: 616
More details: https://www.python.org/dev/peps/pep-0616
4. Type Hinting For Built-in Generic Types
Annotating programs have been made simpler in this release by removing the parallel type hierarchy in Python.
The release has enabled support for the generics syntax in all standard collections currently available in the typing module.
We can use the list or dict built-in collection types as generic types instead of using the typing.List or typing.Dict in the signature of our function.
Therefore, the code now looks cleaner and it has made it easier to understand/explain the code.
PEP: 585
More details: https://www.python.org/dev/peps/pep-0585
5.Improved timezone support In DateTime
The module zoneinfo has been created to support the IANA time zone database. This support for the IANA time zone database has been added to the standard library.
Image for post
PEP: 615
IANA time zones are often called tz or zone info. There are a large number of IANA time zones with different search paths to specify the IANA timezone to a date-time object. As an instance, we can pass in the name of the search path as the Continent/City to a datetime object to set its tzinfo.
dt = datetime(2000, 01, 25, 01, tzinfo=ZoneInfo("Europe/London"))
If we pass in an invalid key then zoneinfo.ZoneInfoNotFoundError will be raised.
https://bugs.python.org/issue30966
7.AsyncIO and multiprocessing Improvements
A number of improvements have been made to the asyncio and multiprocessing library in this release.
As an instance,
The reuse_address parameter of asyncio.loop.create_datagram_endpoint() is no longer supported due to significant security concerns.
New coroutines, shutdown_default_executor() and coroutine asyncio.to_thread() have been added. The shutdown_default_executor schedules a shutdown for the default executor that waits on the ThreadPoolExecutor to finish closing. The asyncio.to_thread() is mainly used for running IO-bound functions in a separate thread to avoid blocking the event loop.
With regards to the multiprocessing library improvements, a new method close() has been added to the multiprocessing.SimpleQueue class.
This method explicitly closes the queue. This will ensure that the queue is closed and does not stay around for longer than expected. The key to remember is that the methods get(), put(), empty() must not be called once the queue is closed.
Link: https://bugs.python.org/issue30966
8. Consistent Package Import Errors
The main issue with importing Python libraries prior to the 3.9 release was the inconsistent import behavior in Python when the relative import went past its top-level package.
The builtins.import() raises ValueError while importlib.import() raises ImportError.
It has been fixed now. The import() now raises ImportError instead of ValueError.
For Further Information: https://bugs.python.org/issue37444
9.String Replace Function Fix
Prior to the Python version 3.9, the “”.replace(“”,s,n) returned empty string instead of s for all non-zero n.
This bug confused the users and caused inconsistent behavior in applications.
The 3.9 release has fixed this issue and it is now consistent with "".replace("", s).
The way replace function works is that for a given max replace occurrence argument, it replaces a set of characters from the string by a new set of characters.
string.replace(s, old, new[, maxreplace])
Return a copy of string s with all occurrences of substring old replaced by new. If the optional argument maxreplace is given, the first maxreplace occurrences are replaced.
To further explain the issue, prior to the version 3.9, the replace function had inconsistent behaviour.
10. Random Bytes Generation
Another feature that has been added in the 3.9 release is the function random.Random.randbytes(). This function can be used to generate random bytes.
We can generate random numbers but what if we needed to generate random bytes? Prior to 3.9 version, the developers had to get creative to generate the random bytes. Although we can use os.getrandom(), os.urandom() or secrets.token_bytes() but we can’t generate pseudo-random patterns.
As an instance, to ensure the random numbers are generated with expected behaviour and the process is reproducible, we generally use the seed with random.Random module.
As a result, random.Random.randbytes() method has been introduced. It can generate random bytes in a controlled manner too.
For Further Information: https://bugs.python.org/issue40286
Originally Published at:https://www.devtipsntricks.com/2020/10/whats-new-in-python-39.html
Buy Siteground Web hosting:
https://www.siteground.com/index.htm?afcode=4bbbeb6048f7d8fd1efec76817755662
Teestring Merchandise:
https://teespring.com/get-coder-s?pid=328&cid=6277&sid=front
Python Tutorial:https://bestpythontutorials.com
My blogs:https://devtipsntricks.com
Instagram:https://instagram.com/skvprogrammer
Twitter:https://twitter.com/skvprogrammer
Facebook:https://facebook.com/satyam.varman.12
My Portfolio:https://skv.netlify.com
My YouTube Channel - [https://www.youtube.com/channel/UC_pPPgwvndi8f2RkbkbvOIA]
Thanks for Reading
Top comments (0)