11

I am writing a program that creates backup of directories.

Which of the following approach is better for code organization?

Create separate class for Backup and Zip logic -

class BackupUtil:
    def backup(self):
       None

class ZipUtil:
    def archive_dir(self):
       None

Or use function nesting like this -

class BackupUtil:
    def backup(self):
        def archive_dir():
            None 
        None

3 Answers 3

11

In general, when the inner function is small and you want to make it clear that it's only useful to the enclosing function. Alternatively, when you need to return a function. The latter scenario is trivial since generally the inner function relies on variables in the enclosing functions's scope, so declaring it anywhere else isn't an option. You might be able to use a lambda in that case, but anything longer than one expression would need a full function declaration.

Without the implementations it's hard to say which one you should choose in this case. I would hasten to add that you don't have to put everything in a class either.

7

TL;DR: Use function nesting when you need the characteristics of function nesting

Function Nesting Use Cases (mostly functional idioms, almost certainly incomplete since it's off the top of my head):

  • closures
  • function factory (programmatic function creation based on parameters)
  • creating functions by calling functool.partial
  • creating functions by using lambda
  • any other reasons you need to create functions during call time

Trade-offs:

  • functions are strongly coupled
  • the code is always called (unless it's in an if block)
  • additional code complexity
  • additional runtime cost (potentially, because the inner function get's re-defined with every call to the outer function)
  • much harder to extend
  • much harder to introspect on the inner function defintion
2
  • 2
    About the "additional runtime cost": I haven't extensively tested it, but some months ago I profiled a code that ran faster when I used a nested function. Finding the nested function may be faster because it's stored in the local scope. Jul 10, 2014 at 20:56
  • All things being equal, redefining a function inside another function is always more expensive than defining it once when the module is loaded. It is the cost of allocating a new function object and binding it to a name whenever the enclosing function is called.
    – snakehiss
    Jul 12, 2014 at 7:42
0

Keeping the two separate would be useful for quite a few reasons:

1) You will have more flexibility working with either class (especially Class inheritance)

2) You can transform the two classes into modules that you can use in your other codes independently from each other

3) You will avoid unnecessarily creating a larger class when you maybe only need to use one of the two features

4) Keeping the functions separate also increases code readability when you later call these functions

Hope that helps clarify somethings.

Your Answer

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

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