Is there a way to set a default parameter equal to another parameter value?

You should do something like :

def perms(elements,setLength=None):
    if setLength is None:
        setLength = elements

Because of the way Python handles bindings and default parameters...

The standard way is:

def perms(elements, setLength=None):
    if setLength is None:
        setLength = elements

And another option is:

def perms(elements, **kwargs):
    setLength = kwargs.pop('setLength', elements)

Although this requires you to explicitly use perms(elements, setLength='something else') if you don't want a default...


No, function keyword parameter defaults are determined when the function is defined, not when the function is executed.

Set the default to None and detect that:

def perms(elements, setLength=None):
    if setLength is None:
        setLength = elements

If you need to be able to specify None as a argument, use a different sentinel value:

_sentinel = object()

def perms(elements, setLength=_sentinel):
    if setLength is _sentinel:
        setLength = elements

Now callers can set setLength to None and it won't be seen as the default.