What is new in python 3.8

bongo-binary blog what is new in python 3.8

What is new in python 3.8

The latest version of python as of writing this article is 3.8 if you are using python bellow this you may want to install newer version. As with any upgrades python3.8 comes with a number of cool features. This post will cover some of those features which we think you should get familiar with.


1. The Walrus operator := Also known as Assignment operator 

Python 3.8 comes with new way of assigning values from expression using (Walrus operator) , with this you don’t have to initialize variables upfront. This feature was availlable in other languages  like Java but in different format. Python3.8 brought it to life. let see the example bellow.

#Before python 3.8
line = file.readline()
while line:
    line = file.readline()

#Let use walrus operator in python 3.8
while line := file.readline():

It's without doubt that the walrus operator has reduced line of codes and save us time and memory.


2. Support for "=" in f-strings

Though there are multiple ways of formating string in python, it's recommended to use f-string whenever necessary.  f-string performs well on string, You should know that f-string where available since python 3.6,  but now we can use "=" in f-string.

For those who are unfamiliar with f-string  f-string are way of embedding python variable and expression values in string, The common format for f-string is: f’{exp}’ where the {exp} will be replaced with the values from expression. Example bellow will give a clear picture, but for those who are familiar with Javascript and Kotlin,  it's basically the same as string interpolation to those languages.

>>> name = "Sophia"
>>> f'Hello there {name}'
'Hello there Sophia'
>>> #Unlike in kotlin or Javascript f-string have no rules to govern the placement of expression.
>>> f'The simple math {3 * 4} and {5/2}'
'The simple math 12 and 2.5'

The new "="

"=" is now added to f-string which works well for debugging purposes. ( Let say you want to print variable name and it's value).

>>> user = "Bongobinary"
>>> f'I love {user=}'
"I love user='Bongobinary'"

Before python3.8 we were not able to use "=" in f-string and if you are like many others who love to print variable name and values during dubugging you will know the benefits of this feature



3. Positional only arguments.

Python 3.8 comes with a way of forcing arguments to only be positional which means function will only be called when these arguments are treated as positional argument and not keyword arguments.

Arguments can be forced to be positional only is by placing “/” after them.


>>> def posarg(a,b,c,/):
...     print(f'Hello {a=} {b=} {c=}')
>>> posarg(3,4,1)
Hello a=3 b=4 c=1


All parameter before the “/” are treated as positional only arguments , turning them into keyword arguements will result into error as:

>>> posarg(a=3,b=3,c=4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: posarg() got some positional-only arguments passed as keyword arguments: 'a, b, c'


Other examples:

>>> def student_grade(language, history, /, name=None):
...     print(f'{language=} {history=} and student name is {name=}')


This means that, student grade function can accept language and history as positional only argument but also allow name to be a keyword argument.

>>> student_grade(90, 80)  # valid
>>> student_grade(50, 60, name="Sophia")  # valid
>>> student_grade(45, 100, 'Sophia')  # valid

>>> student_grade(history=47, language=44)  # invalid TypeError
>>> student_grade(78, name='Johnson')  # invalid  TypeError


Conclussion:These are the top feature of  python 3.8 which we think you should know. Please dont forget to like subscribe and follow @bongobinary social account on twitter, facebook, instagram and youtube.