Python3.8 - Position-Only Parameters

Developers
Python3.8 - Position-Only Parameters

Let's discuss something very interesting and useful improvement introduced in python3.8 (PEP 570), the position-only parameters, This is a function parameter syntax `/ ` to indicate that some function parameters must be specified positionally and cannot be used as keyword arguments.

Example:


In above function parameter are explained as:

pos1, pos2
- position-only parameters


/ - tells us that the parameters appearing before this sign are position-only.

pos_or_kw1, pos_or_kw2 - position-or-keyword parameters

* - tells us that the parameters appearing before this sign are of position-or-keyword type.

kw1, kw2
- keyword-only parameters

  • With positional-or-keyword parameters, the mix of calling conventions is not always desirable. Addition of / marker for positional-only arguments improves the language’s consistency.
  • For the cases where sequence of parameters to the function matters a lot and can not be compromised.
  • There are some cases where parameter names does not really matter and provide no true meaning.

             e.g. min(arg1, arg2)

  • Python library authors would have the flexibility to change the name of positional-only parameters without breaking callers.

Note: Do not use position-only parameters when parameter names have meaning and the function definition is more understandable by being explicit with names, go with keyword-only parameters instead.

   e.g. namedtuple(typenames, field_names, …)


How to use position-only keywords:


different functions calls for `pow` would be like below style:

Outsource the best Development team now!

You know what a great tech team can do to your business. It is time to act fast and outsource the best one. But who to pick or choose? MarsDevs got your back!

We have a team of developers who can build, deploy, and manage applications for you so that you can focus on the business better. As soon as you register, our expertise becomes yours! 

Do you have a project for us? We must connect as you can then share your requirements while we share our experience. You can book your slot now!

Subscribe to our news letter to receive latest updates!

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.