# Function interface for a class (design question)

**URL:** <https://discuss.python.org/t/function-interface-for-a-class-design-question/76353>\
**Category:** Python Help\
**Tags:** help\
**Created:** [January 7, 2025, 10:46am UTC](https://discuss.python.org/t/function-interface-for-a-class-design-question/76353 "2025-01-07T10:46:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pythonnn](https://avatars.discourse-cdn.com/v4/letter/p/7ba0ec/32.png) [@pythonnn](https://discuss.python.org/u/pythonnn)\
**Post date:** [January 7, 2025, 10:46am UTC](https://discuss.python.org/t/function-interface-for-a-class-design-question/76353/1 "2025-01-07T10:46:27Z")

</div>

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:

```python
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!**

---

<div class="post-metadata">

**Author:** ![chepner](https://avatars.discourse-cdn.com/v4/letter/c/22d042/32.png) [@chepner](https://discuss.python.org/u/chepner)\
**Post date:** [January 7, 2025, 2:13pm UTC](https://discuss.python.org/t/function-interface-for-a-class-design-question/76353/2 "2025-01-07T14:13:33Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![pythonnn](https://avatars.discourse-cdn.com/v4/letter/p/7ba0ec/32.png) [@pythonnn](https://discuss.python.org/u/pythonnn)\
**Post date:** [January 7, 2025, 3:07pm UTC](https://discuss.python.org/t/function-interface-for-a-class-design-question/76353/3 "2025-01-07T15:07:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![peterc](https://avatars.discourse-cdn.com/v4/letter/p/f9ae1b/32.png) [@peterc](https://discuss.python.org/u/peterc)\
**Post date:** [January 7, 2025, 3:39pm UTC](https://discuss.python.org/t/function-interface-for-a-class-design-question/76353/4 "2025-01-07T15:39:12Z")

</div>

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

```python
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:

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

```

### store your 6-7 args in a dataclass

```python
@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
  ...

```

---

<div class="post-metadata">

**Author:** ![smontanaro](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/smontanaro/32/1389_2.png) [@smontanaro](https://discuss.python.org/u/smontanaro)\
**Post date:** [January 7, 2025, 4:43pm UTC](https://discuss.python.org/t/function-interface-for-a-class-design-question/76353/5 "2025-01-07T16:43:16Z")

</div>

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

> **[Python Closures: Common Use Cases and Examples – Real Python](https://realpython.com/python-closure/)**
>
> In this tutorial, you'll learn about Python closures. A closure is a function-like object with an extended scope. You can use closures to create decorators, factory functions, stateful functions, and more.

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