Wednesday, December 11, 2019

IslandT: Python Positional-only parameters

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.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...