python not accept keyword arguments

前端 未结 5 1075
萌比男神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:25

    You can use arbitrary argument lists to do this. See http://docs.python.org/tutorial/controlflow.html#arbitrary-argument-lists

    For example:

    def somefunc(*args):
        print args[0], args[1]
    

    Calling without keywords:

    somefunc(10,20)
    

    Gives:

    10 20
    

    Calling with keywords:

    somefunc(a=10,b=20)
    

    Gives an error:

    TypeError: someFunc() got an unexpected keyword argument 'a'
    

    It's unclear why you would want to do this though.

提交回复
热议问题