如何用初始值实例化 class?
How to I instantiate a class with an initial value?
我有这个脚本,我想将循环中的一个值传递给 My_Class:
class My_Class(multiprocessing.Process):
def run(self):
print 'Hello from a class'
return
if __name__ == '__main__':
for x in range(100):
process = My_Class()
process.start()
示例:来自 class 1 的你好;
怎么做这个?
class My_Class(multiprocessing.Process):
def __init__(self, initial_value):
super(My_Class, self).__init__()
v=initial_value
def run(self):
print 'Hello from a class '+self.v
return
if __name__ == '__main__':
for x in range(100):
process = My_Class(x)
process.start()
看来您在 init.d 文件中缺少对超级 class 的调用。虽然我不确定这里的用例是什么,但这段代码对我有用:
import multiprocessing
class My_Class(multiprocessing.Process):
def __init__(self, initial_value):
super(My_Class, self).__init__()
self.v = initial_value
def run(self):
print 'Hello from a class ' + str(self.v)
return
if __name__ == '__main__':
for x in range(100):
process = My_Class(x)
process.start()
我有这个脚本,我想将循环中的一个值传递给 My_Class:
class My_Class(multiprocessing.Process):
def run(self):
print 'Hello from a class'
return
if __name__ == '__main__':
for x in range(100):
process = My_Class()
process.start()
示例:来自 class 1 的你好; 怎么做这个?
class My_Class(multiprocessing.Process):
def __init__(self, initial_value):
super(My_Class, self).__init__()
v=initial_value
def run(self):
print 'Hello from a class '+self.v
return
if __name__ == '__main__':
for x in range(100):
process = My_Class(x)
process.start()
看来您在 init.d 文件中缺少对超级 class 的调用。虽然我不确定这里的用例是什么,但这段代码对我有用:
import multiprocessing
class My_Class(multiprocessing.Process):
def __init__(self, initial_value):
super(My_Class, self).__init__()
self.v = initial_value
def run(self):
print 'Hello from a class ' + str(self.v)
return
if __name__ == '__main__':
for x in range(100):
process = My_Class(x)
process.start()