简体   繁体   English

Python-位置参数紧跟关键字参数

[英]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.

相关问题 PYTHON中的错误位置参数跟随关键字参数 - ERROR IN PYTHON Positional argument follows keyword argument Python celery任务画布:SyntaxError:位置参数紧跟关键字参数 - Python celery task canvas: SyntaxError: positional argument follows keyword argument 位置参数跟随带破折号python的关键字参数图形 - positional argument follows keyword argument graphics with dash python 什么是关键字关键字后面的位置参数? - What is positional argument follows keyword argument? SyntaxError:位置参数跟随关键字参数 - SyntaxError: positional argument follows keyword argument 语法错误:位置参数跟随关键字参数: - Syntax Error: positional argument follows keyword argument: 在决策树可视化过程中,位置参数遵循关键字参数错误 - Positional argument follows keyword argument error during decision tree visualisation 位置参数跟随关键字参数当我尝试分组时出错 - positional argument follows keyword argument Error when i try to group by Dash_table:SyntaxError:位置参数跟随关键字参数 - Dash_table: SyntaxError: positional argument follows keyword argument 位置参数遵循 Django ORM 查询中的关键字参数问题 - positional argument follows keyword argument issue in Django ORM query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM