简体   繁体   中英

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.

class PanSearch(object):    
    otp_wait = 30

    def __init__(self, surname, dob, mobile_no, otp_host, **kwargs):
        kwargs.setdefault('browser', 'chromium')
        self.surname = surname
        self.dob = dob
        self.mobile_no = mobile_no
        self.otp_host = otp_host
        self.middle_name = kwargs.get('middle_name', None)
        self.first_name = kwargs.get('first_name', None)
        self.status = kwargs.get('status')
        self.gender = 'M' if kwargs.get('status') == 'P' else None

# instantiating the object
otp_host = 'abc.xyz.in'
input_kwargs = {'status': 'P', 'gender': 'M', 'browser': 'chromium'}
driver = PanSearch(surname='kulkarni', dob='13/10/1981', mobile_no='9769172006', otp_host, **input_kwargs)

File "pan_no.py", line 87
    driver = PanSearch(surname='kulkarni', dob='13/10/1981', mobile_no='9769172006', otp_host, **input_kwargs)
                                                                                        ^
SyntaxError: positional argument follows keyword argument

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)

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

>>>def print_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

  >>>def print_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)
    1 2 4
    (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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM