I understand passing positional arguments first and then passing the keyword arguments is a rule in python.
And that's why these are wrong:
def fun(x,y):
print(x,y)
fun3(y=4,3)
SyntaxError: positional argument follows keyword argument
and this is wrong too.
def fun2(**kwargs,*args):
File "<stdin>", line 1
def fun2(**kwargs,*args):
^
SyntaxError: invalid syntax
Python strictly checks that I am passing positional arguments first. What I don't understand. Why?
Isn't this intuitive:
def test(x,y,z):
print(x,y,z)
and then calling the function as
test(z=3,1,2)
should first assign keyword argument z
's value 3 and then sequentially assign 1 and 2 to the remaining unassigned variables x and y respectively.
It's not even that python doesn't check if the variable is already assigned or not, because the following gives an error like:
def test2(x,y):
print(x,y)
test2(1,x=1)
TypeError: test2() got multiple values for argument 'x'
Got multiple values for x. So python definitely knows which variable have already received the values. Why can't it then just check for which variables haven't received the values and then assign them those positional argument values sequentially.
I am making notes for my reference and I am stuck while writing anything about the logic behind this behavior of python.
See it seems your argument is valid.
But I like to point out when you said, "Isn't this intuitive:"
Tell me any other object oriented language where you have seen this, and is quite popular.
So the thing is python is popular because it is very easy to adapt to,
I don't know about you but I switched to python recently from C, and there I use to
create functions like length, reverse, sort etc. in python the exact most common names are present of the similar function. It is so easy to adapt, unlike other language where they change name or insert these functions hidden in some utils class like java.
To answering your question. Like other say it will increase complexity.
And this is why other popular object oriented language didn't adopt it.
And so since its not available in other popular language.
Python didn't adopt it, cause it would make adapting to python difficult. And also due to underlying reason that it would make things complex.
"Isn't this intuitive:" for me, politely, NO, it is not intuitive.
If I pass argument at position 0 in function call, it would expect it to go to position 0 argument in function defination. Cause That's how I did in C. And I believe cause that's how its done in most object oriented language.