-2

Say I have a class with a function do_thing that is comprised of multiple steps, which themselves segregate into functions (first_process and second_process). At what point would this be considered bad practice, particularly if you have several composite do_thing functions?

I suspect it clutters the class with functions that aren't obviously linked to the object it represents (they're linked to do_thing). I could pull them to a separate file and import them, but that removes them from the context in which they are called and necessitates lots of switching between fairly small, niche files if someone wants to read whats going on.

Of course, I could also be overthinking this!

Cheers, Tim

class A:
    def init(self, input_data):
        ...
        self.thing = do_thing(input_data)

    def do_thing(self, user_input):
        first_thing = first_process(user_input)
        second_thing = second_process(first_thing)
        return second_thing

    def first_prcoess(self, var):
        ...

    def second_process(self, var):
        ...

1 Answer 1

2

You don't have to put them in a different file, they can either be at top level scope in the same file, or within do_thing.

E.g.

def first_process(self, var):
    ...

def second_process(self, var):
    ...

class A:
    def init(self, input_data):
        ...
        self.thing = do_thing(input_data)

    def do_thing(self, user_input):
        first_thing = first_process(self, user_input)
        second_thing = second_process(self, first_thing)
        return second_thing

or

class A:
    def init(self, input_data):
        ...
        self.thing = do_thing(input_data)

    def do_thing(self, user_input):
        def first_process(var):
            ...

        def second_process(var):
            ...

        first_thing = first_process(user_input)
        second_thing = second_process(first_thing)
        return second_thing

However if you are needing to use self in these functions, having them be part of the class is fine. Maybe you just want to indicate that they are private by naming them _first_process and _second_process

4
  • 1
    Either first_process and second_process don't need a self parameter at all, or they do and they should still be defined in A. You would only define them in do_thing itself if it makes sense for them to close over some argument to do_thing, otherwise you are unnecessarily redefining them every time do_thing gets called.
    – chepner
    Commented May 29 at 11:51
  • @chepner restricting the scope to do_thing for clarity of purpose is worth the redefinitions IMO
    – Caleth
    Commented May 29 at 11:56
  • In your example, they close over self, so really it boils down to whether they need self or not.
    – chepner
    Commented May 29 at 12:50
  • 1
    @chepner no, it also matters whether you want the name first_process defined outside of do_thing or not
    – Caleth
    Commented May 29 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.