Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 24336

IslandT: Python Positional-only parameters

$
0
0

I have downloaded Python 3.8 and start to play around with those latest python functions. In this article, we will look at the Positional-only parameter syntax which is a function parameter syntax / to indicate that some function parameters must be specified positionally and cannot be used as keyword arguments which means after the / syntax we may specify a value for each parameter within that function. For example,

def f(a, b, /, c, d):
    print(a, b, c, d)
f(10, 20, 30, d=40)

The below example will print out the sum of all the parameters within that function.

import math  

def e(a):
    return a * a

def f(a, b, /, **kwargs):
    sum = a + b 
    for num in kwargs:
        sum += kwargs[num]
    print(sum)

f(2, 3, c=40, d=e(10), e=math .sin(60)) # output 144.695

The above syntax has been contributed by Pablo Galindo. Do you think the syntax is useful? Leave your comment below this post.


Viewing all articles
Browse latest Browse all 24336

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>