什么是Python中嵌套类的替代方法

Chr*_*yes 2 python multithreading nested-class

我读了一篇帖子,说'嵌套类不是pythonic'是什么选择

请原谅我,这不是最好的例子,但它是基本概念.用于执行任务的嵌套类.我基本上不得不连接多个线程的服务.

import threading, imporedlib

class Mother(threading.Thread):
    def __init__(self,val1,val2):
        self.VAL1 = val1
        self.VAL2 = val2
    def connectandrun():
        for i in range(5):
            Child.run(i)
    class Child:
        def run(self):
            importedlib.runajob(Mother.VAL1, Mother.VAL2)
Run Code Online (Sandbox Code Playgroud)

bit*_*den 7

你想使用组合:

import threading, importedlib

class Child:
    def __init__(self, parent):
        self.parent=parent

    def run(self):
        importedlib.runajob(parent.VAL1, parent.VAL2)



class Mother(threading.Thread):
    def __init__(self,val1,val2):
        self.VAL1 = val1
        self.VAL2 = val2

    def connectandrun():
        c= Child(self)
        for i in range(5):
            c.run(i)
Run Code Online (Sandbox Code Playgroud)

当然,名字"母亲"和"孩子"在这里不再合适,但你明白了.