0

There is a common way to pass arguments in Python like this:

def foo(a1, a2):
    print(a1, a2)
    
def my_dict():
    return {'a1': 4, 'a2':5}

foo(**my_dict())

...

4 5

But now I want to move the asterisks inside my_dict function:

def foo(a1, a2):
    print(a1, a2)
    
def my_dict():
    return **{'a1': 4, 'a2':5}

foo(my_dict())

...

SyntaxError: invalid syntax

Is there a way to achieve such behavior in Python? I am dealing with third-party code and can modify my_dict function, but can't modify foo(my_dict()) call.

Ars ML
  • 49
  • 4
  • 1
    `**` is part of the function calling syntax. You can't move it into the function, "unpacked dictionary" is not a thing. – Barmar Apr 15 '23 at 12:25
  • Can you modify `foo`? You need to change it so it just gets a single argument. – Barmar Apr 15 '23 at 12:27
  • If it calls `foo(my_dict())`, it expects `foo()` to be defined like `def foo(my_dict):`. The function is expected to extract the dictionary elements itself, not unpack them in the parameter list. – Barmar Apr 15 '23 at 12:31

0 Answers0