Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
什么';它是Python中嵌套类的替代方案_Python_Multithreading_Nested Class - Fatal编程技术网

什么';它是Python中嵌套类的替代方案

什么';它是Python中嵌套类的替代方案,python,multithreading,nested-class,Python,Multithreading,Nested Class,我读过一篇文章,说“嵌套类不是pythonic的”,还有什么选择呢 请原谅,这不是最好的例子,但它是基本概念。用于执行任务的嵌套类。我基本上必须在多个线程中连接到一个服务 import threading, imporedlib class Mother(threading.Thread): def __init__(self,val1,val2): self.VAL1 = val1 self.VAL2 = val2 def connectand

我读过一篇文章,说“嵌套类不是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)

您要使用合成:

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)

当然,“母亲”和“孩子”这两个名字在这里已经不太合适了,但是你明白了。

你希望从你编写的代码中得到什么结果?