1

I have a method, which takes a lot arguments as input. Something like this, but with more args:

foo.add(a=True, b=0, c='z', d=(1, 0, 0), e=2, f='g')
foo.add(a=False, b=3, c='z', d=(1, 0, 2), e=2, f='z')
foo.add(a=True, b=10, c='h', d=(1, 3, 0), e=2, f='2')

I decided to make a dictionary and add each line's args into that. But I can not use it like this:

foo_dict = {
    "a": "a=True, b=0, c='z', d=(1, 0, 0), e=2, f='g'",
    "b": "a=False, b=3, c='z', d=(1, 0, 2), e=2, f='z'",
    "c": "a=True, b=10, c='h', d=(1, 3, 0), e=2, f='2'",
}
foo.add(foo_dict["a"])
foo.add(foo_dict["b"])
foo.add(foo_dict["c"])

What is the best way to pass the args from another variable and not getting error?

2
  • Close. If the inner strings were a dict, you could use a double splat (unpacking). foo.add(**foo_dict["a"]) Commented Feb 2, 2023 at 20:59
  • See Python args and kwargs: Demystified. Commented Feb 2, 2023 at 21:01

1 Answer 1

1

Make each value a dict, then you can unpack it with **.

foo_dict = {
    "a": {'a':True, 'b':0, 'c':'z', 'd':(1, 0, 0), 'e':2, 'f':'g'},
    "b": {'a':False, 'b':3, 'c':'z', 'd':(1, 0, 2), 'e':2, 'f':'z'},
    "c": {'a':True, 'b':10, 'c':'h', 'd':(1, 3, 0), 'e':2, 'f':'2'},
}
for v in foo_dict.values():
    foo.add(**v)
Sign up to request clarification or add additional context in comments.

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.