1

NOTE I am not creating the inner_function - I am trying to call a specific function within mine and this seemed to be the easiest way to explain it without making is specific to the library I am using.

Here is what I am trying to do more specifically. This will be the two functions:

def inner_function(parameter1='nothing', parameter2='nothing', parameter3='nothing'):
    print(parameter1)
    print(parameter2)
    print(parameter3)

def outer_function(parameter_set=<default value>):
    inner_function(parameter_set)

First, when I call outer_function() I expect the output to be:

nothing
nothing
nothing

I do not know what the <default value> should be to accomplish this.

Second, if I want the output to be:

nothing
wamo
thunk

What would the argument passed into outer_function() be?

Sorry if this is explained in a confusing manner or has been asked before but I am having trouble finding a post answering this.

I have not tried much for the default value - only None and empty []

3
  • It might make sense to use keyword arguments. That way you can specify only the arguments you want to provide, and automatically pick up the default values for the others. Commented Feb 6, 2023 at 1:30
  • 1
    inner_function(parameter_set) This passes one and only one argument. parameter_set will be passed as parameter1. Nothing at all will be passed as parameter2 and parameter3, so those will receive their default values. Commented Feb 6, 2023 at 1:51
  • 1
    Are you able to use the **kwargs syntax for calling inner_function()? Or are you locked in to the example you showed. Commented Feb 6, 2023 at 1:52

1 Answer 1

2

As someone said in the comments, you can use keyword arguments:

def inner_function(parameter1='nothing', parameter2='nothing', parameter3='nothing'):
    print(parameter1)
    print(parameter2)
    print(parameter3)

def outer_function(parameter_set={}):
    inner_function(**parameter_set)

Some tests:

>>> k1 = {"parameter2": "foo"}
>>> k2 = {
...  "parameter1": "YEs",
...  "parameter2": False,
...  "parameter3": (1,2,3),
... }
>>> inner_function()
nothing
nothing
nothing
>>> outer_function()
nothing
nothing
nothing
>>> outer_function(k1)
nothing
foo
nothing
>>> outer_function(k2)
YEs
False
(1, 2, 3)

Just remember that anything can get passed in the kwargs variable, if you want to limit it to your variables, you should add some code in outer_function to check it before running inner_function

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

2 Comments

This is the right answer. My only suggestion is that your usage example would be even better if you showed that some of the arguments could be omitted from the dictionary in order to still get the default values (i.e. show that you can do outer_function({"parameter2": "wamo", "parameter3": "thunk"}), which the questioner asked for).
Good suggestion, I'll edit k1 to do this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.