Function argument's default value equal to another argument [duplicate]

偶尔善良 提交于 2019-12-29 07:47:30

问题


Is it possible to define a function argument's default value to another argument in the same function definition? Something like:

def func(a, b=a):
  print a, b

but that didn't work.


回答1:


No. This is not possible. The Python interpreter thinks that you want to assign the default value of argument b to a global variable a when there isn't a global variable a.

You might want to try something like this:

def func(a, b=None):
    if b is None:
        b = a


来源:https://stackoverflow.com/questions/33527745/function-arguments-default-value-equal-to-another-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!