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

后端 未结 5 1859
栀梦
栀梦 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.

    0 讨论(0)
  • 2020-12-04 11:14

    There is really no benefit to doing this, except if you are dealing with metaclasses.

    the class: suite really isn't what you think it is. It is a weird scope, and it does strange things. It really doesn't even make a class! It is just a way of collecting some variables - the name of the class, the bases, a little dictionary of attributes, and a metaclass.

    The name, the dictionary and the bases are all passed to the function that is the metaclass, and then it is assigned to the variable 'name' in the scope where the class: suite was.

    What you can gain by messing with metaclasses, and indeed by nesting classes within your stock standard classes, is harder to read code, harder to understand code, and odd errors that are terribly difficult to understand without being intimately familiar with why the 'class' scope is entirely different to any other python scope.

    0 讨论(0)
  • 2020-12-04 11:18

    You might want to do this when the "inner" class is a one-off, which will never be used outside the definition of the outer class. For example to use a metaclass, it's sometimes handy to do

    class Foo(object):
        class __metaclass__(type):
            .... 
    

    instead of defining a metaclass separately, if you're only using it once.

    The only other time I've used nested classes like that, I used the outer class only as a namespace to group a bunch of closely related classes together:

    class Group(object):
        class cls1(object):
           ...
    
        class cls2(object):
           ...
    

    Then from another module, you can import Group and refer to these as Group.cls1, Group.cls2 etc. However one might argue that you can accomplish exactly the same (perhaps in a less confusing way) by using a module.

    0 讨论(0)
  • 2020-12-04 11:19

    I don't know Python, but your question seems very general. Ignore me if it's specific to Python.

    Class nesting is all about scope. If you think that one class will only make sense in the context of another one, then the former is probably a good candidate to become a nested class.

    It is a common pattern make helper classes as private, nested classes.

    0 讨论(0)
  • 2020-12-04 11:19

    There is another usage for nested class, when one wants to construct inherited classes whose enhanced functionalities are encapsulated in a specific nested class.

    See this example:

    class foo:
    
      class bar:
        ...  # functionalities of a specific sub-feature of foo
    
      def __init__(self):
        self.a = self.bar()
        ...
    
      ...  # other features of foo
    
    
    class foo2(foo):
    
      class bar(foo.bar):
        ... # enhanced functionalities for this specific feature
    
      def __init__(self):
        foo.__init__(self)
    

    Note that in the constructor of foo, the line self.a = self.bar() will construct a foo.bar when the object being constructed is actually a foo object, and a foo2.bar object when the object being constructed is actually a foo2 object.

    If the class bar was defined outside of class foo instead, as well as its inherited version (which would be called bar2 for example), then defining the new class foo2 would be much more painful, because the constuctor of foo2 would need to have its first line replaced by self.a = bar2(), which implies re-writing the whole constructor.

    0 讨论(0)
提交回复
热议问题