0

I have a dataclass like this

@dataclass
class Pkg:
    func: typing.Any

I have 3 funcs like

def func1(A, B, C):
   # do something
def func2(A, P):
   # do something
def func3(D):
   # do something

I created a wrapper pkg for each function like this

pkg1 = Pkg(func=func1)
pkg2 = Pkg(func=func2)
pkg3 = Pkg(func=func3)

Then I'm trying to call each of these in another function like

def run_func(pkg, args):
    pkg.func(args)

However, each of them take a different number of params, which is obscured by the wrapper function. What's the best way to write this?

A1122
  • 1,158
  • 2
  • 11
  • 26
  • It depends on how you intend to use `run_func` and where you expect the parameter values to come from. Given that `args` is a tuple of arguments to use, you can call `pkg.func(*args)`. Depending on the context, you might either receive that tuple directly, or use `*args` in the `run_func` signature. See the linked duplicate for an overview of the topic, and then make the design decision appropriate for your context. Incidentally, if you're using the `typing` library then you may as well be specific about types where possible - `typing.Callable` exists for this purpose. – Karl Knechtel Dec 16 '21 at 03:29

0 Answers0