Python3.8 New Features

Python3.8 New Features

Assignment expressions (the walrus operator)

new syntax := used in larger expression to assign values to a variables.

Example,

if (a := (b*c)) > 100):

It is affectionately known as “the walrus operator” due to its resemblance to the eyes and tusks of a walrus.

In Python 3, raw_input function has been deprecated and replaced by the input function and is used to get a string through the keyboard.

Example python3.8 program for walrus operator,

assignment_expr.py

#/usr/bin/python

if (number := int(input("Enter Number:")))%2 == 0:
    print("Input number ", number," is even!")
else:
    print("Input number ", number," is odd!")

Output:

# python assignment_expr.py
Enter Number:10
Input number  10  is even!
# python assignment_expr.py
Enter Number:7
Input number  7  is odd!

Lets see how this new assignment expresstion operator can be used in list comprehensions.

#/usr/bin/python

programming_languages = ["c", "c++", "python", "java", "ruby"]
updated_names = ["c+", "python+"]

result = [lang for language in programming_languages
          if(lang := (language+"+")) in updated_names]

print(result)

Output:

# python assignment_expr1.py 
['c+', 'python+']

Positional only arguments

New function argument '/' syntax is to indicate that some function parameters must be specified positionally and cannot be used as keyword arguments.

def func(a1, a2, /, b1, b2, *, c1, c2):
    print(a1, a2, b1, b2, c1, c2)

In this example, a1 and a2 are positional only parameters , while b1 or b2 can be positional or keyword arguments, and c1 or c2 are required to be keywords arguments.

Valid example above function call,

f(11, 12, 13, b2=14, c1=15, c2=16)
[root@localhost ~]# cat positional-only.py
#/usr/bin/python

def compute(a1, a2, /, b1, b2, *, c1, c2):
    return ((a1+a2)*(b1+b2))/(c1-c2)

val1 = compute(10, 20, 30, 40, c1=15, c2=5)
print("compute func result is ", val1)

val2 = compute(10, 20, 30, b2=40, c1=15, c2=5)
print("compute func result is ", val2)

val3 = compute(10, 20, b1=30, b2=40, c1=15, c2=5)
print("compute func result is ", val3)

#val4 = compute(10, a2=20, b1=30, b2=40, c1=15, c2=5)
#val4 = compute(10, 20, b1=30, 40, c1=15, c2=5)
#print("compute func result is ", val4)

Output:

[root@localhost ~]# python positional-only.py
compute func result is  210.0
[root@localhost ~]# vi positional-only.py
[root@localhost ~]# python positional-only.py
compute func result is  210.0
compute func result is  210.0
[root@localhost ~]# vi positional-only.py
[root@localhost ~]# python positional-only.py
compute func result is  210.0
compute func result is  210.0
compute func result is  210.0
[root@localhost ~]# vi positional-only.py
[root@localhost ~]# python positional-only.py
compute func result is  210.0
compute func result is  210.0
compute func result is  210.0

Invalid call,

#val4 = compute(10, a2=20, b1=30, b2=40, c1=15, c2=5)
#val4 = compute(10, 20, b1=30, 40, c1=15, c2=5)
#print("compute func result is ", val4)

Error:

Traceback (most recent call last):
  File "positional-only.py", line 15, in 
    val4 = compute(10, a2=20, b1=30, b2=40, c1=15, c2=5)
TypeError: compute() got some positional-only arguments passed as keyword arguments: 'a2'
[root@localhost ~]# vi positional-only.py
[root@localhost ~]# python positional-only.py
  File "positional-only.py", line 15
    val4 = compute(10, 20, b1=30, 40, c1=15, c2=5)
                                  ^
SyntaxError: positional argument follows keyword argument

Parallel filesystem cache for compiled bytecode files

PYTHONPYCACHEPREFIX setting (-X pycache_prefix) configures the implicit bytecode cache to use a separate parallel filesystem tree instead of default __pycache__ subdirectories within each source directory.

Debug build uses the same ABI as release build

Python3.8 uses the same ABI irrespective of built in release or debug mode.

f-strings support =

used for self-documenting expressions and improves debugging.

Added an = specifier to f-strings.

f-string such as f'{expr=}' will expand to the text of the expression as 'expr=' format in string.

Example python3.8 program for f-strings support '=',

#/usr/bin/python

if (number := int(input("Enter Number:")))%2 == 0:
    print(f'Input {number=} is even!')
else:
    print(f'Input {number=} is odd!')

Output:

[root@localhost ~]# python f-strings.py 
Enter Number:10
Input number=10 is even!
[root@localhost ~]# python f-strings.py 
Enter Number:7
Input number=7 is odd!

Support of \N{name} escapes in regular expressions

\n{name} escapes support is added in regular expressions.

#/usr/bin/python
import re

text = "testing  © 2020"
result = re.compile(r'\N{copyright sign}\s*(\d{4})')
print(int(result.search(text).group(1)))

Output:

[root@localhost ~]# python escapes.py
2020




Python installation

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^