BongoBinary

Positional only arguments python 3.8

By Aloyce Bahati11 months ago5 minutes read

Positional only parameters

Positional only arguments in python python 3.8

python 3.8 introduced positional only arguments. positional only arguments is a way of enforcing functions and methods arguments to be passed positionally only.

A marker '/' is passed after all arguments which are supposed to be positional only.

 

Example:

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

 

 

Code segment above declare a function named student_grade. student_grade function accepts three arguments ( language, history and name) language and history has been marked as positional only arguments by placing / after those arguments.  The name args remain to be keyword argument. Code bellow will show valid and invalid way of calling student_grade function.

 

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:

While there might be a lot of benefits for using positional only arguments but top benefit is consistency. i hope this article provided enough explanation about positional only argument. Please dont forget to like subscribe and follow @bongobinary social account on twiter, facebook, instagram and youtube.

 

Check how to install python 3.8 on ubuntu

 

Related articles:

How to install python 3.8 on ubuntu    How to install python 3.8 on ubuntu
What is new in python 3.8    Whats new in python