In Python, can I specify a function argument's default in terms of other arguments?

后端 未结 3 1378
南方客
南方客 2020-12-01 15:55

Suppose I have a python function that takes two arguments, but I want the second arg to be optional, with the default being whatever was passed as the first argument. So, I

3条回答
  •  春和景丽
    2020-12-01 16:21

    The second way is fine in my opinion: it is exactly clear what you do, and for all those who will come after you and read the code. While there is a slight overhead in documenting the default behavior, the use of 'None' or any other to make your function generate a default value is very common. Putting logic inside the function calls would certainly not be commendable in my opinion, it could become very complex very soon. So just leave it in the function body, where all the code is.

提交回复
热议问题