Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 668
Alex Avatar asked Nov 04 '15 17:11

Alex


1 Answers

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
like image 178
Ethan Bierlein Avatar answered Oct 11 '22 13:10

Ethan Bierlein