[英]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. 我把
kwargs
作为字典通过。 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: 当我们使用(* keyword)时,它将收集剩余的position关键字,例如:
>>>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 如我们所见,(* argument)将提供的值放在一个元组中,并且不会收集关键字。如果要收集关键字argument,则可以使用(** argument)来实现,例如
>>>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: 您可以得到想要的东西,但是在使用(** augment)时,最好注意输入,例如:
>>>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
由于(** argument)仅收集关键字参数,因此您定义的函数包含参数名称(x,y,z),然后输入参数(x =,y =,z =),这会导致(* *参数)和您的关键字argumet,因此,如果您想解决问题,建议您更改单词
>>>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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.