什么';它是Python中嵌套类的替代方案
我读过一篇文章,说“嵌套类不是pythonic的”,还有什么选择呢 请原谅,这不是最好的例子,但它是基本概念。用于执行任务的嵌套类。我基本上必须在多个线程中连接到一个服务什么';它是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
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)
当然,“母亲”和“孩子”这两个名字在这里已经不太合适了,但是你明白了。你希望从你编写的代码中得到什么结果?