Forced naming of parameters in Python

后端 未结 11 1286
日久生厌
日久生厌 2020-11-27 12:15

In Python you may have a function definition:

def info(object, spacing=10, collapse=1)

which could be called in any of the following ways:<

11条回答
  •  长情又很酷
    2020-11-27 12:56

    You could declare your functions as receiving **args only. That would mandate keyword arguments but you'd have some extra work to make sure only valid names are passed in.

    def foo(**args):
       print args
    
    foo(1,2) # Raises TypeError: foo() takes exactly 0 arguments (2 given)
    foo(hello = 1, goodbye = 2) # Works fine.
    

提交回复
热议问题