Why use packed *args/**kwargs instead of passing list/dict?

后端 未结 3 1982
南笙
南笙 2021-01-31 21:12

If I don\'t know how many arguments a function will be passed, I could write the function using argument packing:

def add(factor, *nums):
    \"\"\"Add          


        
3条回答
  •  走了就别回头了
    2021-01-31 22:01

    It's about the API: *args provides a better interface, as it states that the method accepts an arbitrary number of arguments AND that's it - no further assumptions. You know for sure that the method itself will not do anything else with the data structure containing the various arguments AND that no special data structure is necessary.

    In theory, you could also accept a dictionary with values set to None. Why not? It's overhead and unnecessary. To me, accepting a list when you can accept varargs is adding overhead. (as one of the comments pointed out)

    Furthermore, varargs are a good way to guarantee consistency and a good contract between the caller and the called function. No assumptions can be made.

    When and if you need a list, then you know that you need a list!

    Ah, note that f(*args) is not the same as f(list): the second wants a list, the first takes an arbitrary number of parameters (0 included). Ok, so let's define the second as an optional argument:

    def f(l = []): pass
    

    Cool, now you have two issues, because you must make sure that you don't modify the argument l: default parameter values. For what reason? Because you don't like *args. :)

    PS: I think this is one of the biggest disadvantages of dynamic languages: you don't see anymore the interface, but yes! there is an interface!

提交回复
热议问题