2
相關:pickle load error "__init__() takes exactly 2 arguments (1 given)"的Python:泡菜的錯誤:__new __()到底需要2個參數(1給出)
import cPickle as pickle
pklfile = "test.pkl"
all_names = {}
class Name(object):
def __new__(cls, c, s="", v=""):
name = "%s %s %s" % (c, s, v)
if all_names.has_key(name):
return all_names[name]
else:
self = all_names[name] = object.__new__(cls)
self.c, self.s, self.v = c, s, v
return self
with open(pklfile, 'wb') as output:
pickle.dump(Name("hi"), output, pickle.HIGHEST_PROTOCOL)
with open(pklfile, 'rb') as input:
name_obj = pickle.load(input)
OUTPUT:
Traceback (most recent call last):
File "dopickle.py", line 21, in <module>
name_obj = pickle.load(input)
TypeError: __new__() takes at least 2 arguments (1 given)
是否有可能使這項工作,而無需第二個參數作爲可選項?