Is there a benefit to defining a class inside another class in Python?

StackOverflow

What I"m talking about here are nested classes. Essentially, I have two classes that I"m modeling. A DownloadManager class and a DownloadThread class. The obvious OOP concept here is composition. However, composition doesn"t necessarily mean nesting, right?

I have code that looks something like this:

class DownloadThread:
    def foo(self):
        pass

class DownloadManager():
    def __init__(self):
        dwld_threads = []
    def create_new_thread():
        dwld_threads.append(DownloadThread())

But now I"m wondering if there"s a situation where nesting would be better. Something like:

class DownloadManager():
    class DownloadThread:
        def foo(self):
            pass
    def __init__(self):
        dwld_threads = []
    def create_new_thread():
        dwld_threads.append(DownloadManager.DownloadThread())




Get Solution for free from DataCamp guru