Skip to content Skip to sidebar Skip to footer

Python- Positional Argument Follows Keyword Argument

I have a function which accepts variable length of arguments as described below. I am passing the kwargs as a dictionary. However I don't understand why I am getting the error. cla

Solution 1:

you need to change

driver = PanSearch(surname='kulkarni', dob='13/10/1981', mobile_no='9769172006', otp_host, **input_kwargs)

to

driver = PanSearch('kulkarni', '13/10/1981', '9769172006', otp_host, **input_kwargs)

Solution 2:

when we use (*keyword) ,it will collect the remaining position keyword,for exmple:

>>>defprint_1(x,y,*z):
       print(x,y,z)
>>>print_1(1,2,3,4,5,6)
(1,2,(3,4,5,6,7))

as we can see th( *argument) put the provided value in a tuple,and it won't collect the keyword .If you want to collect the keyword argument ,you can use (**argument) to achieve,like

  >>>defprint_paramas(x,y,z=3,*pospar,**paramas):
            print(x,y,z)
            print(pospar)
            print(paramas)
    >>>print_paramas(1,2,4,445,8889,36,foo=5,br=46,sily=78)
    124
    (445, 8889, 36)
    {'foo': 5, 'br': 46, 'sily': 78}

you can get what you want ,but when you use(**augument), you'd better pay attention to your importation,example:

>>>print_paramas(x=1,y=2,z=4,445,8889,36,foo=5,br=46,sily=78)
SyntaxError: positional argument follows keyword argument

why? Because (**argument) only collect the keyword argument ,the fuction you defined contains the argument names(x,y,z) ,and you input the argument(x=,y=,z=),it causes cofliction between (**argument) and your keyword argumet ,so if you want to solve your problem , I suggest you to change the word

>>>driver = PanSearch(surname='kulkarni', dob='13/10/1981', mobile_no='9769172006', otp_host, **input_kwargs)

to Follow a sequence

>>>driver = PanSearch('kulkarni', '13/10/1981','9769172006', otp_host, **input_kwargs)

Post a Comment for "Python- Positional Argument Follows Keyword Argument"