0

Issue: I have 2 functions that both require the same nested functions to operate so they're currently copy-pasted into each function. These functions cannot be combined as the second function relies on calling the first function twice. Unnesting the functions would result in the addition of too many parameters.

Question: Is it better to run the nested functions in the first function and append their values to an object to be fed into the 2nd function, or is it better to copy and paste the nested functions?

Example:

def func_A(thing):

    def sub_func_A(thing):
        thing += 1

    return sub_func_A(thing)

def func_B(thing):

    def sub_func_B(thing):
        thing += 1

    val_A, val_B = func_A(5), func_A(5)

    return sub_func_B(val_A), sub_func_B(val_B)

Imagine these functions couldn't be combined and the nested function relied on so many parameters that moving it outside and calling it would be too cluttered

7
  • 2
    Why do these need to be sub-functions? In this example they could be made free functions without changing any of the interfaces. Commented Feb 18, 2022 at 15:02
  • In this case that's true, but in my case, they rely on parameters that come from computationally heavy functions which I want to avoid recalling if I were to move them outside Commented Feb 18, 2022 at 15:04
  • I think the burden of proof is on you to show how computationally heavy functions and too many parameters are an actual problem. Commented Feb 18, 2022 at 15:05
  • 1
    Is it possible, you could make the nested function accept a function as an argument? Your func_A and func_B would accept all the parameters and the sub_func would accept either func_A or func_B. Commented Feb 18, 2022 at 15:13
  • 2
    Could you use place the functions in a class instead? func_A, func_B could be public (i.e. used in the interface), while the sub_func could be private (used only by other functions in the class). The object instance could hold values calculated by other class methods so these computed values would not have to be passed as parameters thus avoiding the clutter. Commented Feb 18, 2022 at 15:13

1 Answer 1

1

The "better option" depends on a few factors -:

  • The type of optimization you want to achieve.
  • The time taken by the functions to execute.

If the type of optimization to be achieved here is based on the time taken to execute the second function in the two cases, then it depends on the time taken for the nested function to fully execute, if that time is less than the time taken to store it's output when it's first called by the first function then its better copy pasting them.

While, if the time taken by the nested function to execute is more than the time taken to store it's output, then its a better option to execute it first time and then store it's output for future use.

Further, As mentioned by @DarylG in the comments, a class based approach can also be used wherein the nested function(subfunction) can be a private function(only accessible by the class's inner components), while the two functions(func_A and func_B) can be public thus allowing them to be used and accessed widely from the outside as well. If implemented in code it might look something like this -:

class MyClass() :
    def __init__(self, ...) :
        ...
        return
    
    def __subfunc(self, thing) :
        # PRIVATE SUBFUNC
        thing += 1
        return thing
    
    def func_A(self, thing):
        # PUBLIC FUNC A
        return self.__subfunc(thing)
    
    def func_B(self, thing):
        # PUBLIC FUNC B
        val_A, val_B = self.func_A(5), self.func_A(5)
        return self.__subfunc(val_A), self.__subfunc(val_B)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.