Python-位置参数跟随关键字参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44940683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python- positional argument follows keyword argument
提问by Akhilesh Chobey
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.
我有一个函数,它接受可变长度的参数,如下所述。我正在将它kwargs
作为字典传递。但是我不明白为什么我会收到错误。
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
回答by Rahul
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)
回答by I can explain
when we use (*keyword) ,it will collect the remaining position keyword,for exmple:
当我们使用 (*keyword) 时,它会收集剩余的位置关键字,例如:
>>>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
如我们所见,th( *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:
你可以得到你想要的,但是当你使用(**augument)时,你最好注意你的输入,例如:
>>>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=),它会导致 (* *argument) 和你的关键字参数,所以如果你想解决你的问题,我建议你改变这个词
>>>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)