2

I was wondering if I could something like this in Python:

def func(val1,val2=val1):
    #do whatever you want

I could do the following:

def func(val1,val2=None):
    val2 = val1 if val2 == None else val2
    #do whatever you want

But is there a more efficient/sexier way to do it?

Edit: I don't think the linked question is close to what I am asking. What I am searching for is the right way to set a function's default parameter value to the value of another parameter

Xema
  • 1,796
  • 1
  • 19
  • 28

1 Answers1

1

Checking if a parameter is None is done often in python where the parameter could be a mutable type.

By checking if none, it ensures that if the default type is a mutable type, an existing object isn't used.

tl;dr, what you have seems pretty pythonic to me.

Sayse
  • 42,633
  • 14
  • 77
  • 146