Understanding Python 3.8 Version

Posted By : Piyush Khandelwal | 03-Jan-2020

Python's lastest version 3.8 is available in beta since 14th October, 2019 . It adds new syntax to the language, a few minor changes, and mostly a bunch of speed improvements. This article outlines some of the most significant additions and changes you should know about Python 3.8.

Let's take a look!

In this article, you’ll learn about:

  • Assignment Expression - The Walrus Operator.
  • Positional Only Arguments.
  • f-strings with “=”.
  • New syntax warnings.

1. Assignment Expression - The Walrus Operator

The biggest feature in Python 3.8 is the introduction of assignment expressions. They are written using a new notation (:=). This is affectionately called “the walrus operator” because of its resemblance to the eyes and tusks of a walrus. This operator allows you to assign and return a value in the same expression.

For example, if you would like to assign to a variable and print its value, then you typically do something like this:

>>> name = 'Oodles'

>>> print(name)

        Oodles

In the latest version of python , you can combine these two statements into one, using the walrus operator:

>>> print(name := 'Piyush')

        Piyush

The expression allows you to assign 'Piyush' to walrus, and immediately print the value. But remember that the walrus operator does not do anything that isn’t possible without it.

Another scenario that shows some of the strengths of the walrus operator.Example:

>>> line = f.readline()

>>> while line:
            ...  # process line
            line = f.readline()

can be written in a single statement like:

>>> while line := f.readline():
            ...  # process line

However, some might say that this affects the readability of the code, as the former is clearer and explicit than the latter. So use your best judgment about when to use the walrus operator.

2. Positional Only Arguments

When defining a method now you can use a special marker, /, to specify that the method only accepts positional arguments on the left of the marker. In Python, keyword arguments are already available with the * marker, and thus the addition of / marker for positional-only arguments improves the language’s consistency and allows for robust API design.

Take an example of this function:

>>> def pow(x, y, z=None, /):
           r = x ** y
           if z is not None:
           r %= z
           return r

By adding / after z, you specify that x,y,z is a positional-only argument. Even though z is a keyword argument but because it is before / marker, Python will consider z as a positional argument. You can combine keyword arguments with positional ones by placing the keyword arguments after the slash, For Example:

>>> def pow(x, y, /, z=None):
           r = x ** y
           if z is not None:
           r %= z
           return r

3. f-strings with “=”

As a Python programmer, I frequently use “printf-style” debugging:

>>> foo = 'Oodles'

>>>print ("foo=", foo)

       foo=Oodles

f-strings make this a bit easier and readable:

>>>f"foo={foo}"

       foo=Oodles

But you still have to repeat yourself: you have to first write out the string “foo”, and then the expression “foo”.

The = specifier, used as f"{expr=}" expands to the text of the expression, an equal sign, then the repr of the evaluated expression. So now, this will be equivalent to the above statement:

>>>f"{foo=}"

      foo=Oodles

You can also add some spaces around =, and use format specifiers as usual:

>>>f"{foo = }"

      foo = Oodles

>>>f"{foo =  :>10}"

      foo =          Oodles

 

4.New Syntax Warnings

In Python 3.8, The interpreter throws a SyntaxWarning in some cases when a comma is missed before tuple or list. So when you accidentally do this:

>>>data = [
      (1, 2, 3)  # oops, missing comma!
      (4, 5, 6)
      ]

Instead of showing TypeError: 'tuple' object is not callable which doesn’t tell you what’s wrong, a helpful warning will be shown pointing out that you probably missed a comma. Pretty helpful while debugging! The compiler now also produces a SyntaxWarning when identity checks (is and is not) are used with certain sorts of literals (e.g. strings, integers, etc.). You rarely want to compare identities with literals other than None, and a compiler warning can help avoid several elusive bugs.

Conclusion

The latest release of python added some great new features and added some significant improvements to the performance of the language, there is a small number of behavior changes that might require modifying existing code while upgrading to Python 3.8, but the performance gains and new syntax make it worth the effort.

So, now you have learned about some of the new features in Python 3.8. If you'd like to learn more about them, please visit the following links:

Related Tags

About Author

Author Image
Piyush Khandelwal

Piyush is a bright web developer. He is Bachelor in Computer Applications. He has good knowledge of Python,Odoo, Flectra and Django Framework.

Request for Proposal

Name is required

Comment is required

Sending message..