Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python- positional argument follows keyword argument

Tags:

python

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
like image 587
Akhilesh Chobey Avatar asked Jul 06 '17 05:07

Akhilesh Chobey


People also ask

Do positional argument follows keyword argument?

The Positional arguments should always appear first, followed by the keyword arguments.

Can keyword arguments be followed by positional arguments Python?

Keyword arguments are passed to functions after any required positional arguments. But the order of one keyword argument compared to another keyword argument does not matter. Note how both sections of code below produce the same output.

What is positional argument and keyword argument in Python?

A positional argument is a name that is not followed by an equal sign (=) and default value. A keyword argument is followed by an equal sign and an expression that gives its default value.

How does Python solve positional arguments?

Positional ArgumentsThe first positional argument always needs to be listed first when the function is called. The second positional argument needs to be listed second and the third positional argument listed third, etc. An example of positional arguments can be seen in Python's complex() function.


2 Answers

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)
like image 80
Rahul Avatar answered Oct 13 '22 00:10

Rahul


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)
like image 30
I can explain Avatar answered Oct 13 '22 00:10

I can explain