Python 中嵌套类的替代方案是什么
我读过一篇文章,指出“嵌套类不是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)
I read a post stating that 'nested classes wasn't pythonic' what's the alternative
please forgive me, this isn't the best example but it's the basic concept. a nested class for performing a task. I'm basically having to connect to a service in multiple threads.
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你想要使用组合:
当然,“母亲”和“孩子”这个名字在这里不再合适,但你明白了。
You want to use composition:
Of course the names "Mother" and "Child" are not really appropriate anymore here, but you get the idea.