python not accept keyword arguments

前端 未结 5 1092
萌比男神i
萌比男神i 2021-01-12 08:13

I am trying to make my code NOT to accept keyword arguments just like some bulitins also do not accept keyword arguments, but, I am unable to do so. Here, is my thinking acc

5条回答
  •  一个人的身影
    2021-01-12 08:41

    There isn't such a strong distinction in Python between keyword and positional arguments. In fact, it is as simple as:

    Positional arguments are parsed in the order they are listed in the function call Keyword arguments can be added in any order, but cannot come before positional arguments.

    So, if you have this:

    def foo(x):
        pass
    

    You can do:

    foo(1) # positional
    foo(x=1) # keyword
    

    But you cannot do foo() because you didn't supply a default value.

    Similarly, if you have this:

    def foo(x,y='hello'):
        pass
    
    foo(1) # this works fine
    foo() # this is invalid, as an argument without a default isn't passed
    foo(y='hello',1) # error - positional arguments come before keyword arguments
    

    Don't forget to read up on arbitrary argument lists.

提交回复
热议问题