How to Set a Default Argument from a Previous Argument

Can I set a default argument from a previous argument?

The answer is no, you can't. You could get the behaviour you want using overloads:

void f(int a, int b, int c);
inline void f(int a, int b) { f(a,b,b); }
inline void f(int a) { f(a,a,a); }

As for the last question, C doesn't allow default parameters at all.

Can a C++ default argument be initialized with another argument?

Another argument cannot be used as the default value. The standard states:

8.3.6 Default arguments
...

9 A default argument is evaluated each time the function is called with no argument for the corresponding
parameter. The order of evaluation of function arguments is unspecified. Consequently, parameters of a
function shall not be used in a default argument, even if they are not evaluated.

and illustrates it with the following sample:

int f(int a, int b = a); // error: parameter a
// used as default argument

How to define default argument value based on previous arguments?

Can I do this in the method definition

No. During the method definition there's no way to know what value x might have at run-time. Default arguments are evaluated once at definition time, there's no way to save a dynamic value for y.

or should I do something like resize(x,y=None) and check inside

exactly. This is a common idiom in Python.

How can I make the default value of an argument depend on another argument (in Python)?

The language doesn't support such syntax.

The usual workaround for these situations(*) is to use a default value which is not a valid input.

def func(n=5.0, delta=None):
if delta is None:
delta = n/10

(*) Similar problems arise when the default value is mutable.

Get values of default arguments based on previous argument

Simply assign default value, and check if it's valid inside function:

function theRange($min, $max = 5) {
$max = $min > $max ? $min : $max;
}

An argument with default value precedes non-default argument in JavaScript

It'd only be possible by explicitly passing undefined, and by also providing a default for the second argument.

function func(a = 10, b) {
return a + b;
}

console.log(func(undefined, 5));

Default value in function parameter retained from previous function call

What's happening?

When python sees

def print_from_stream(n, stream=EvenStream()):

It evaluates EvenStream() as the default parameter. This is one, definite, object. Each time you call print_from_stream with a default parameter, it uses the same instance that was created when EvenStream() was first evaluated. print_from_stream now has an associated object default_stream. This object is never updated with a new EvenStream. In C++ lingo, you've created a static variable.

The Fix

Make the default parameter of print_from_stream something like None. Then, add

if stream is None:
stream = EvenStream()

to the top of your function. This will make a new instantiation of EvenStream() every time you call your function with a default parameter.

Note that its slightly different than before, as print_from_stream(10, None) will now work and provide even numbers, as opposed to crashing. This is still accepted in Python as good practice, similar to how passing undefined in a JS function is the same as if you didn't give any parameters. It lets users skip over the 2nd argument to get to the third as well, if they don't want to name their parameters.

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


Related Topics



Leave a reply



Submit