Welcome to Python 3.8

Srijan Sahay
Nov 1 · 4 min read

Python 3.8 is the latest major release of the Python programming language, and it contains many new features and optimizations.

Python 3.8

Some of the new features of Python 3.8 are:

1. The walrus operator

Walrus

We have a new kind of assignment operator in this new version called Walrus operator (:=). This operator has the advantage of setting and returning a variable in one line. For example:

Code:
sample_data = [
{“id”: 1, “name”: “Srijan”},
{“id”: 2, “name”: “Abhishek”},
{“id”: 3, “name”: “Dilip”},
{“id”: 4, “name”: “Vishal”},
{“id”: 4, “name”: None},
]
print(“With Python 3.8 Walrus Operator:”)
for entry in sample_data:
if name := entry.get(“name”):
print(f’Found name = {name}’)
print(“Without Walrus operator:”)
for entry in sample_data:
name = entry.get(“name”)
if name:
print(f’Found name = {name}’)
Output:
With Python 3.8 Walrus Operator:
Found name = “Srijan”
Found name = “Abhishek”
Found name = “Dilip”
Found name = “Vishal”
Without Walrus operator:
Found name = “Srijan”
Found name = “Abhishek”
Found name = “Dilip”
Found name = “Vishal”

2. Positional-only arguments

A special marker ‘/’ can now be used when defining a method’s arguments to specify that the function only accepts positional arguments on the left of the marker. The ‘/’ marker here means that passing values for x, y and z can only be done positional, and not using keyword arguments.

Code:
def func1(x,y,z=None,/):
r = x + y
if z is not None:
r -= z
return r
print(func1(2, 5)) #7
print(func1(2, 5, 3)) #4
print(func1(x=2, y=10)) #TypeError
print(func1(2, 10, z=3)) #TypeError

3. f-string also support ‘=’

Now you can use ‘=’ operator within f-string to get and print value.
This mean that now we can perform our arithmetic operations within f-string.

a=5
b=6
print(f'sum={a+b}') #11

4. New syntax warning

Python have introduced new warning messages for missing comma along with the error messages in this new version. The interpreter will throw this helpful warning message which will help user to quickly find their error.

list1=[[0,1] [2,3]] #this will give a SyntaxWarning of missing comma along with the TypeError.
SyntaxWarning

5. Reversible dictionary

reversed() now works with dictionary. Python has started preserving the order of key inserted in dictionary from 3.7 but now in 3.8 you can also reverse it.

dict1={'a':5,'b':6}
print(dict1) #{'a': 5, 'b': 6}
print(list(reversed(dict1))) ['b', 'a']

6. Multiprocessing shared memory

Using multiprocessing, the data can be shared and accessed globally across all the instances of Python. This will speed up saving, storing, accessing, and transferring data with much ease.

7. New protocol for creating pickle files

Pickle in Python is primarily used in serializing and de-serializing data, and code that can be used somewhere else also. In this new version, Python will be using Protocol 4 by default and you can also use Protocol 5 with it. This will result APIs to take full advantage, hence improving the performance.
For people working in field of data science, this is an exciting news as this will work well with Django and Flask even in the servers with low or limited space.

8. Improved CPython

Python has also said that they have improved Python’s C engine. This will make optimizations easy in Python. People from data science field had encountered many instance where the Python or Jupyter environment crashed because of huge amount of data. With improvement in its CPython module, user can expect better result in processing and querying data.

9. Performance improvement

This release have added number of performance that speeds-up the interpreter. Some of them are:

  • operator.itemgetter() is now 33% faster.
  • Field lookups in collections.namedtuple() are now more than two times faster.
  • list constructor now create list of 12% smaller on average.
  • uuid.UUID now uses slots to reduce it’s memory footprint.
  • Invocation of some simple built-ins and methods are now 20–50% faster.

and many more…
To know more, check https://www.python.org/downloads/release/python-380/


Summary

The upcoming release of Python adds some great new features to the language and significantly improves the performance with fundamental speed-up fixes. Let us welcome Python 3.8 and make the best use of it.

The Startup

Medium's largest active publication, followed by +536K people. Follow to join our community.

Srijan Sahay

Written by

Data Scientist, Technologist, Educator LinkedIn: https://www.linkedin.com/in/srijansahay/

The Startup

Medium's largest active publication, followed by +536K people. Follow to join our community.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade