Awesome New Python 3.8 Features

Awesome New Python 3.8 Features

Get to know the useful new python 3.8 features

There are a number of new Python features have been added to the version 3.8. However, some of the features are simply awesome. This article will outline the new features of Python 3.8 which you should be familiar with.

1. Assignment Expressions :=

It is also known as walrus operator.

It assigns values to variables as part of an expression without initialising them up front.

if (my_variable := get_input()) == 100:
    perform_action_as_my_variable_is_hundred();

Here, the variable “my_variable” has been assigned a value.

This feature is unavailable in list comprehensions and other expression contexts. It is only available in statement forms.

2. Positional-only parameters (/)

If you want to call a function in Python that accepts parameters then you can pass the arguments by position or by keyword. But what if we want to restrict the callers of our API to only call our function by passing in parameters by position?

The positional-only parameter functionality solves it:

def add(a, b, c, d=None, /):
    x = a+b+c
    if d is not None:
        x = x+d
    return x

As a consequence, add(1,2,3) and add(1,2,3,4) are valid calls/

However, add(a=1,b=2,c=3) or add(1,2,3,d=4) are all invalid calls.

3. For Debugging Ease, f-strings now support =

The specifier = can be added to the f-strings now.

f -strings are in the form of f'{expr=}' Notice the ‘=’. The equal sign can help us evaluate the expression:

input = 100
output = 50
print(f'{input-output=}')

Would print 100-50=50.

This makes debugging really easy as the function is evaluated and the output is produced.

4. functools.lru_cache can be added as a decorator

functools.lru_cache is great for recursive calls. A dictionary is used to cache the results. We can now add the functools.lru_cache() decorator

@lru_cache
def my_function(input):
    some io bound or recursive function

5. Reversed(sequence) Is Introduced

A number of times, I wanted to reverse an iterator. Luckily, reversed(input) is now added for Dict and dictviews objects. The *input *argument in the reversed function must have a reversed() function, OR it must have the len() and getitem() method whereby the arguments start at 0.

Photo by Nick Fewings on Unsplash

That’s All Folks

Read the **[changelog ](https://docs.python.org/3.8/whatsnew/changelog.html#changelog "changelog ")for complete list**

Helpful Articles

If you want to understand Python from start to end then read this article: Everything About Python — Beginner To Advanced

Summary

This article provided a summary of the awesome Python 3.8 features.

Hope it helps.

python

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

How To Compare Tesla and Ford Company By Using Magic Methods in Python

Magic Methods are the special methods which gives us the ability to access built in syntactical features such as ‘<’, ‘>’, ‘==’, ‘+’ etc.. You must have worked with such methods without knowing them to be as magic methods. Magic methods can be identified with their names which start with __ and ends with __ like __init__, __call__, __str__ etc. These methods are also called Dunder Methods, because of their name starting and ending with Double Underscore (Dunder).

Python Programming: A Beginner’s Guide

Python is an interpreted, high-level, powerful general-purpose programming language. You may ask, Python’s a snake right? and Why is this programming language named after it?

Hire Python Developers

Are you looking for experienced, reliable, and qualified Python developers? If yes, you have reached the right place. At **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")**, our full-stack Python development services...

Python any: How to Check If Element is Iterable or Not

Python any() function returns True if any element of an iterable is True otherwise any() function returns False. The syntax is any().