The recent Python 3.8 release introduced many interesting new features, optimizations and tweaks. As new Python versions are published, developers must stay up to date with language changes. In this article, we will try to present the most exciting changes.
Assignment operator
The assignment operator lets you create value assignments within larger logic expressions.
Example
>>> if ( value := 1 + 1 ) > 1: # 2 > 1
print(value)
2
>>>
In this example, we assign 1+1 to a variable (value) and check if it is greater than 1. It is important to note that you can also use an assignment operator inside list comprehension.
New f-strings specifier
Python 3.8 introduces a new ‘=’ specifier available to use inside f-string expressions. The specifier will automatically get the object name with the object value paired.
Example
>>> object='example_value'
>>> f'{object}'
'example_value'
>>> f'object={object}'
'object=example_value'
>>> f'{object=}'
"object='example_value'"
>>> f'{object=!s}'
'object=example_value'
>>>
This new feature will certainly be helpful in testing and logging. As you can see in the example, it is also possible to use this conversion flag with the new specifier.
New importlib.metadata module
New importlib.metadata module can help you read metadata, such as the version number for packages installed with tools like pip. Let’s check the installed version of Django and its requirements.
Example
>>> from importlib import metadata
>>> metadata.version('django')
'3.0'
>>>
>>> metadata.requires('django')
['pytz', 'sqlparse (>=0.2.2)', 'asgiref (~=3.2)', "argon2-cffi (>=16.1.0) ; extra == 'argon2'", "bcrypt ; extra == 'bcrypt'"]
>>>
Removed features in Python 3.8
- The macpath module
- isAlive() method of threading.Thread
- platform.popen() method
Deprecated features in Python 3.8
- getchildren() and getiterator() methods of ElementTree
- lgettext(), ldgettext(), lngettext() and ldngettext() of gettext module
To sum up, if your career path includes Python development, it is worth keeping up to date with python releases systematically. Check out our blog for more news.