We use cookies on our website, hope you don’t mind. Just imagine a plate of delicious meringues, they look like clouds. Read more

Development

Python 3.8 – what you might have missed

It’s finally out! OK, fair enough, it’s old news now, as the Python 3.8 release date was October 14th, 2019. So you might have already had enough time to check out all the changes and had a good think about their impact. While most blog posts focus on expression assignment, aka the walrus operator (for good reason, as it is a big deal indeed), our team decided to take a closer look on the updates which might have slipped your attention, but which you may find particularly useful. This post was a joint work of mine and my teammates – Radosław Kowalski and Anna Rudzińska.

This blog post is for you if: 

  • You’re a Python user and want a handy summary of the 3.8 release
  • You want an insight into Python 3.8 features and useful updates that might have slipped your attention

Why us? 

  • We work as Python developers at 10Clouds, each of us having 1-3 years of experience in Python
  • We always aim to be at the cutting edge of the latest tech developments and to test them on practical examples
  • We take part mostly in startup and new projects, with constantly changing and developing APIs, requiring fast and easy to maintain solutions

f-strings support = for self-documenting expressions and debugging

Don’t Repeat Yourself… unless you have to. Sometimes you do. Luckily, Python 3.8 has just eliminated one of those situations by adding a new feature to its beloved (at least by me) f-strings. You do know f-strings, right? They have been with us since 3.6 and to be honest, I couldn’t live without them:

The old school way:

The first option is much easier to understand, isn’t it? So what’s new here? In Python 3.8 we can use a syntax f”{variable=} to print the variable name and and its value at once. Instead of writing f”variable={variable}” we can do f”{variable=}”, like here:

When is this useful? In debugging and more precisely in debug logging, when you just want to quickly log or print a variable to investigate a bug. Imagine the following scenario:

send_request(domain, endpoint, **params) is our simple function. Let’s check it out:

Something is clearly wrong with our API key, we want to log some values to help us investigate. That’s easy:

OK, we’ve found out that our environment variable is not set, but we had to write a lot of duplicated code for it: domain: {domain}, endpoint: {endpoint} etc. But with the new {variable=} we can avoid it. F-string f'{domain=}’ would translate into domain=’www.website.com’. What’s more, as with normal F-strings, we can use string formatting:

Now when we run:

Useful, isn’t it?

Sync-up of dict comprehensions with dict literals

The next useful change that was introduced in Python 3.8 is a sync-up of dict comprehensions with dict literals — the key is computed first and the value second. Firstly, let’s have a look at how it worked in the previous Python version:

As we see, in the case of dict comprehension the value is computed first (“song?”) and then the key (“band?”). In dict literals it is other way round — first the key, then the value. Python 3.8 to the rescue! With the sync-up, dict comprehension and dict literals behave the same way:

This execution order is especially useful when paired up with another new feature — assignment expressions. If you assign a variable in the key expression, it will be available in the value expressions:

Added support of \N{name} escapes in regular expressions

Contributed by Jonathan Eunice and Serhiy Storchaka in bpo-30688 as enhancement given following explanation: ​

The re module specially handles Unicode escapes (\uXXXX and \UXXXXXXXX) so that even raw strings (r’…’) have symbolic Unicode characters. But it has not supported named Unicode escapes such as r’\N{EM DASH}’, making the escapes for string literals and the escapes for regular expressions asymmetric.

First of all it is important to note that \N{unicode symbol name} escape was already available in string literals, which some of you may not have known.

Any idea what symbol the Unicode hex code below refers to? \N not only allows you to easily provide Unicode symbols but also increases code readability as opposed to using \uXXXX and \UXXXXXXXX escapes.

No luck? Let me help you a bit: 

Take a look at the below, which might be helpful: 

Now, next time you’re searching for a strange character, just take its name and let ‘re’ handle the rest! But first of all, make sure that you’ve updated to Python 3.8. 

In summary…

Presented above are only few of the changes introduced with Python 3.8, which our team have found useful and interesting. There is a lot more to discover and you can find the release summary in the official release notes (no surprise, starting with Assignment expressions). A full list of changes and features can be found in the official changelog together with links to specific issues and bug reports.

One notable change that we found on the list and did not discuss here is releasing asyncio.run() as stable.

This function can be used to execute a coroutine and return the result while automatically managing the event loop. For example:

So as you can see, checking out release notes for minor versions of Python may help you find new useful tools for tackling everyday problems and even find out about features that you have missed in previous releases.

Finally, if you would like to read about walrus operator take a look at this short article from Better Programming (or any other, there are plenty). Even better, read PEP 572 with full rationale behind this addition and all no-goes so you never use it wrong!

Looking to start a new project in Python? We can help. Just contact our friendly team on hello@10clouds.com and we’ll get back to you as soon as possible. 

Michał Karaś

you may also like these posts

Blog image

Remote HR interviews – how to prepare and stand out

Blog image

The long-term benefits of DevOps

Blog image

Flutter Vs React Native – Which is better for your business?

Free project quote

Fill out the enquiry form and we'll get back to you as soon as possible.

  • Google
  • Facebook/Twitter/LinkedIn
  • Behance/Dribble
  • Clutch/GoodFirms/AppFutura
  • Blog/Medium
  • Referral
  • Events
  • Other

Thank you for your message!


We’ll get back to you in 24 hours.

Meanwhile take a look at our blog.

Read our blog
Gareth N. Genner Photograph

Gareth N. Genner

Co-Founder of Trust Stamp

Quote

We needed a partner who could take on our idea, and make it real. 10Clouds bring so many different skills. We feel that every member that’s involved in the project is a member of our team.