Max Alekseyev
Max Alekseyev

Reputation: 173

passing keyword argument to inner function when outer function has keyword argument with the same name

I have two functions defined roughly like this:

def func_inner(bar=0):
    print('inner bar:', bar)
    
def func_outer(bar=-1, *args, **kwargs):
    print('outer bar:', bar)
    func_inner(*args, **kwargs)

Is there a way to call func_outer and provide it with two values of bar - one for func_outer, and the other to be passed over to func_inner? Calling func_outer(bar=1,bar=2) clearly does not work.

It's possible to overcome the issue by specifying bar values as positional arguments, like func_outer(1,2), but I'd like to specify them as keyword arguments for both functions.

Is there a solution entirely at the caller's side (ie. without altering the functions)?

Upvotes: 2

Views: 591

Answers (1)

tbrugere
tbrugere

Reputation: 1623

No, there is none

You cannot pass two arguments with the same name to a function. Thus, you will not be able to have the key "bar" in kwargs.

Thus you cannot pass this argument without modifying the two functions.

There may be a more adapted way to what you’re doing

This is the kind of code that pops up in a decorator. In this kind of case, you may want to make the outer function currified.

Upvotes: 1

Related Questions