2

I am trying to write code that does not repeat itself, following the DRY principle.

Consider a function call with many arguments, both mandatory and optional. In some cases, I would like to specify a value for an optional argument, whereas in other cases I would like to leave that value to its default value. To simplify my problem:

def func(a, b=2):
    print("b = {}".format(b))

avalue = 1
condition = 2
arg = None  # <-- Means: "use default" 
if condition == 1:
    arg = 3

func(avalue, b=arg)

Output:

b = None

Expected output:

b = 2

Thus, I am trying to avoid coding the function call twice like this:

if arg:
    func(avalue, b=arg)
else:
    func(avalue)

Is it possible in Python?

5 Answers 5

4

Use a dictionary, and only set the optional argument as a key-value pair in that if you want to specify it. Then apply the dictionary using the **kwargs call syntax:

avalue = 1
condition = 2

kwargs = {}
if condition == 1:
    kwargs['b'] = 3

func(avalue, **kwargs)

An empty dictionary (the condition != 1 case) leaves b set to the default value.

Sign up to request clarification or add additional context in comments.

Comments

1

One of the solutions can be:

def func(a, b=2, **kwargs):
    print("b = {}".format(b))

avalue = 1
condition = 2

func(avalue, **{} if condition != 1 else {'b':3})

Comments

0

What about :

def func(a, b=None):
    if not b:
        b = 2
    print("b = {}".format(b))

avalue = 1
condition = 2
arg = None  # <-- Means: "use default" 
if condition == 1:
    arg = 3

func(avalue, b=arg)

At this point however the default isn't really used the way it should be, defaults are usually kept for when you have two different methods, one that works in a way that the other shouldn't for example for addition/subtraction with a switch:

def maths(a,b,sub=false):
    result = a + b
    if sub:
        result = a - b
    return result

1 Comment

Good suggestion, but I cannot modify func since it is a library function
0

How about this:

def func(a, b=None):
    b = 2 if not b else b
    print("b = {}".format(b))

avalue = 1
condition = 2
arg = None  # <-- Means: "use default"
if condition == 1:
    arg = 3

func(avalue, arg)  # b = 2

and if condition = 1:

func(avalue, arg)  #  b = 3

The above assumes you can modify the function definition. If that is not the case you have to go with **kwargs.

1 Comment

In most cases I would prefer this over the **kwargs trick. Stuff in the stdlib does this. with if b is None: b = 2 though
0

Op has mentioned that the function func can't be modified so my sugguestion would be :

def func(a, b=2):
    print("b = {}".format(b))

avalue = 1
condition = 2
if condition == 1:
    func(avalue, b=3)
else:
    func(avalue)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.