How to Set a Default Parameter Equal to Another Parameter Value

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

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.

Default Parameter Value to Another Parameter

Is this possible?

No. Default values for parameters have to be one of:

  • A compile time constant (e.g. a numeric or string literal)
  • The default value for the parameter type, e.g. default(Foo) or default (as of C# 7.1)
  • The "zero" values of value types, e.g. new Guid()

It's quite restrictive, unfortunately.

Python - how to set default parameter equal to another parameter

How about this?

def foo(arg1, arg2, src, dst=None):
dst = dst if dst is not None else src

Test:

>>> def foo(arg1, arg2, src, dst=None):
... dst = dst if dst is not None else src
... print dst
...
>>> foo(0, 0, "test")
test

Following @TanveerAlam 's comment, I don't want to make assumptions about your arguments (what if dst can be False?), I did use shorter versions in my original post and I'll leave them here for reference:

def foo(arg1, arg2, src, dst=None):
dst = dst if dst else src

Or even:

def foo(arg1, arg2, src, dst=None):
dst = dst or src

Note that these versions will prevent values other than None in dst (like False, [] ...)

Function argument's default value equal to another argument

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

Sets the default value of a parameter based on the value of another parameter

This is a pretty standard pattern:

def consecutive_generator(size=20, start=0, end=None):
if end is None:
end = size + start

Python Argparse - Set default value of a parameter to another parameter

Normally, single dash flags are single characters. So -ca is unwise, though not illegal. In normal POSIX practice it could be interpreted as -c a or -c -a.

Also argparse allows flagged arguments (optionals) to occur in any order.

Parsing starts out by assigning all the defaults. When the relevant flag is encountered, the new value overwrites the default. Given that order, it's impossible for one argument to take on the value of another as a default.

In general interactions between arguments are best handled after parsing. There is a mutually_exclusive grouping, but no mutually_inclusive. You can build in some sort of interaction via custom Action classes, but implementing that is just as much work as any post-parsing testing.

In sum, the simplest thing is to use the default default, None, and test

if args.ca is None:
args.ca = args.c

How can a parameter's default value reference another parameter?

This requires that you use multiple parameter lists:

case class A(x: Int)(y: Int = x*2)

Default values can only refer to parameters in preceding lists.

Be careful however with case classes, because their equality only takes into the account the first parameter list, therefore:

A(1)() == A(1)(3)  // --> true!!

Set default function parameter equal to another field of that function

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.



Related Topics



Leave a reply



Submit