# How to implement "positional-only parameter" in a user defined function?

def fun( a, b, /): print(a,b)

o/p:

File "<ipython-input-36-1fceb840aeb9>", line 1 def fun( a, b, /): ^ SyntaxError: invalid syntax

edit retag close merge delete

Sort by ยป oldest newest most voted

I am not sure to understand your question, but does the following inspire you ?

sage: def fun( a, b, c=None):
....:     print(a,b)
sage: fun(2,3)
(2, 3)
sage: fun(2,3,4)
(2, 3)


or even

sage: def fun( a, b, *args):
....:     print(a,b)
sage: fun(2,3)
(2, 3)
sage: fun(2,3,4)
(2, 3)
sage: fun(2,3,4,5)
(2, 3)

more

( 2018-10-16 13:10:12 +0100 )edit

help(pow)

o/p:
-----
Help on built-in function pow in module builtins:

pow(x, y, z=None, /)
Equivalent to x**y (with two arguments) or x**y % z (with three arguments)

Some types, such as ints, are able to use a more efficient algorithm when
invoked using the three argument form.


I want to know can we write a user defined function like pow (), which has positional only parameter.

( 2018-10-16 13:16:38 +0100 )edit

Examples:

 def fun( a, b):
print(a**b)
fun(a=5, b=2)   # 25
fun(b=2,a=5)    # 25
fun(5,2)        # 25

def fun( a, b, /):
print(a**b)
fun(5,2)        # 25
fun(a=5, b=2)   # should show error

( 2018-10-16 13:30:20 +0100 )edit

ok i got it . Those are built-in functions (builtin_function_or_method), not user-defined functions (function). They are implemented in C, into python library. These functions are not exposed at the Python level.

more