What’s New in Beta Version of Python 3.8!
The newest version Python 3.8 is going to launch soon in its stable form. While there is still some time for it to launch officially as developers are still testing out and removing bugs, let’s look at what’s new in Python 3.8!
Now there a number of features that have been added to the latest version of Python, some of them are added to increase coding efficiency while others are included because they are simply awesome. Let’s look at them one by one:
The Walrus Operator
The Walrus sign will be used as a syntax that will help us to assign values to variables which has larger expression or more lines of code (conditions). For example if you want to introduce a variable and later assign a condition to it; you can do it in a single line:
num1 = 10 if (num2 := num1) > 11: print("Num2 is greater than Num1)
The value of num1 is assigned to 10. The walrus operator := is assigned to num2 to the value of num1 which is 10 in our case. The if condition box along with a ‘walrus‘ operator allows us to not only compare the values with 11 but it also equates num2 with num1.
Positional Only Argument
As we know that a function can have multiple arguments both positional and keyword arguments. However, in Python 3.8 that’s not the case as now we can restrict the structure of how to use positional, positional or keyword and keyword arguments respectively. With the new /
syntax, we can create differentiation and clarification between arguments. For example in order to add positional, positional or keyword and keyword arguments then use this syntax:
def func(x, /, y, z): pass
x: positional argument
y: positional or keyword argument
z: keyword argument only
Now we can only pass certain values after the /
syntax as now we have clearly shown the positional only arguments on the left side i.e. they will be contained before the /
. And, whatever comes after the /
will be positional or keyword or only keyword argument. We can call the function now by following methods:
func('apple', 'mango', z='papaya') func('apple', x='mango', z='papaya')
x is set to mango on line 2 as it can be both positional or keyword argument.
reversed() method
The reversed()
built-in can now be used to access the dictionary in the reverse order of insertion. For example:
>>> d = dict(a=1, b=2) >>> list(reversed(d)) ['b', 'a'] >>> list(reversed(d.keys())) ['b', 'a'] >>> list(reversed(d.values())) [2, 1] >>> list(reversed(d.items())) [('b', 2), ('a', 1)]
This change does add built-in types but I think it’s a reasonable expectation as a python user to be able to do reversed(dict(a=1, b=2) since the order is know defined in the specifications. It seems inconsistent to have an order on dict, views and not have reversed work on them.