Function interface for a class (design question)

how should i do best to make a function interface that wraps a “private” class?

i have this function i want to use let’s call it func(), and the point is to expose it via package __init__, but the core functionality is done using a class, so it wraps a class and does some stuff

how should i best do this? i prefer not to write all the parameters twice since the PrivateClass will only ever be used internally.

my code below is how i do it now:

def PrivateClass:
    def __init__(params):
        self.takes = params.get("takes")
        # and so on ...


def func(takes=None, a=None, bunch=None, of=None, parameters=None):
    # do some stuff
    params = locals()
    PrivateClass.run(params)

the other way i can do it is by only using self.params.get(“mykey”) where i need it in the class.


additional question, when making a private class for internal use should it be started with a _, such as _PrivateClass?

please all give your opinion on the best way to do this!

Why is PrivateClass a class in the first place? Your use of the class in func doesn’t fit with the definition shown (you don’t instantiate the class, only use a static/class method with an argument seemingly intended for __init__), but there’s enough to suggest that PrivateClass should probably just be a function itself. (It can still be private; perhaps there is some difference in how the user would call func and how func would call the private implementation.)

to add more context, i wish i could write this as functions but i always needed to pass around tons of state to make it work, it is unreasonable to pass 6-7 args everywhere all the time.

therefore i have concluded, it’s best to write a function that has the relevant parameters (this will be the api that you import in the package) then it passes this on to class which does most of the work

the question of this topic is “how to make a function interface for a class the best way?” without writing all the parameters twice. I currently do it by passing locals(), then taking what i need from that.


additional note: it is private because the func does enough preparation that you would never want to use PrivateClass by yourself even if you could

To expose a method of a class only, I think I would use

Class _MyPClass:
  ...
  def my_method(...):...

public_interface = _MyPClass.my_method

It’s a bit hacky, and it does have downsides, but it works.

For the problem you’re describing, I could imagine other solutions though:

Use sub-functions:

def public_function(...):
  ...
  def private_function_one():
    "I have access to 'local' variables :)"
    ...

store your 6-7 args in a dataclass

@dataclass(slots=True, frozen=True)
Class Args:
  arg1: [type]
  arg2: [type]
  arg3: [type]
  arg4: [type]
  arg5: [type]
  arg6: [type]

def public_function(*args):
  aaargs = Args(*args)
  x = _private_function(aaargs, ...)

def _private_function(aaargs, ...):
  aaargs.arg1
  ...

Perhaps not the best alternative, but closures can be useful for better data hiding. The Real Python peeps posted an article about them recently:

(Sorry, can’t do much about the required login…)