使用 pp 模块时无法更新对象“测试”中的字典“参数”。谁能告诉我为什么会这样?只看代码:
import pp
class test(object):
params = {'n': None}
dic2 = {}
n = None
def __init__(self, i):
#won't change
self.params['n'] = i
#changed
self.n = i
self.dic2 = {i: i}
def run(self):
print self.n, self.params, self.dic2
job_server = pp.Server()
jobs = []
for i in xrange(10):
t = test(i)
#won't change
t.params['n'] = i
#changed
t.n = i
t.run()
jobs.append(job_server.submit(t.run))
[job() for job in jobs]
结果:
0 {'n': 0} {0: 0}
1 {'n': 1} {1: 1}
2 {'n': 2} {2: 2}
3 {'n': 3} {3: 3}
4 {'n': 4} {4: 4}
0 {'n': None} {0: 0}
1 {'n': None} {1: 1}
2 {'n': None} {2: 2}
3 {'n': None} {3: 3}
4 {'n': None} {4: 4}
正如我们在使用 pp 时看到的,“params['n']”无法更新。这是一种奇怪的行为。这怎么可能发生?