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

后端 未结 5 1875
栀梦
栀梦 2020-12-04 10:46

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 conc

5条回答
  •  情歌与酒
    2020-12-04 11:05

    You could be using a class as class generator. Like (in some off the cuff code :)

    class gen(object):
        class base_1(object): pass
        ...
        class base_n(object): pass
    
        def __init__(self, ...):
            ...
        def mk_cls(self, ..., type):
            '''makes a class based on the type passed in, the current state of
               the class, and the other inputs to the method'''
    

    I feel like when you need this functionality it will be very clear to you. If you don't need to be doing something similar than it probably isn't a good use case.

提交回复
热议问题