0

I am looking for a way to input named function arguments as one named list instead of specifying them individually when using the function.

Let's say I have this:

import numpy as np

class Something:

    __init__(self, one = None, two = None, three = None, **kwargs):
        self.one = one
        self.two = two
        self.three = three

    def sum(self):
        np.sum(self.one, self.two, self.three)

Right now, I have to do something like this:

Instance = Something(one = 1, two = 2, three = 3)
Instance.sum()

But what I want to do is something like this:

Inputs = {'one' : 1, 'two': 2, 'three': 3, 'four': 4}
Instance = Something(Inputs)
Instance.sum()

Do you have any solution for this?

3
  • 5
    Something(**Inputs)? And that's a dictionary, not a "named list". Commented Jan 11, 2024 at 15:13
  • 2
    You would also have to add a def to your contractor. Commented Jan 11, 2024 at 15:15
  • Wow, @jonrsharpe, that was quick, thank you! Commented Jan 11, 2024 at 15:15

1 Answer 1

1

Use ** to expand a dictionary into your function args.

>>> def foo(bar, baz): return bar + baz
...
>>> foo(baz=1, bar=2)
3
>>> foo(**{'baz':1, 'bar':2})
3
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.