![](/img/trans.png)
[英]Object oriented python: calling a class with method(self, *optargs) and receiving error “takes at most 2 arguments (4 given)”
[英]method of class receiving unknown arguments
我有一个自己定义的类,该类当前有2个参数,self和savepath。 此类的方法接受2个参数self。 在该方法中,我调用了一个函数,该函数再次使用了两个参数,local_hash和filename,但是,在调用该方法时,出现了以下错误。 我认为这与自我论点有关,但我不知道在哪里或为什么。 作为记录,put_nowait()是默认模块的方法。 我不认为我需要发布我正在使用的所有相关默认模块的代码。
def cache_files(self, path):
self.folder_path = path
self.md5_queue = Queue.Queue()
accepted_file_types = ['.jpg', '.png', '.gif']
self.hash_directory = os.walk(self.folder_path, topdown=True)
if self.folder_path != None:
for root, subfolders, images in self.hash_directory:
for filename in images:
try:
if filename[-4:] in accepted_file_types:
self.local_hash = hash_sum(os.path.join(root, filename))
self.md5_queue.put_nowait(filename, self.local_hash)
except IOError:
continue
print 'Directory has finished caching, exiting...'
return self.md5_queue
def run(self):
# references pickle file if available
md5_path = os.path.join(os.path.dirname(__file__), 'md5.pickle')
try:
self.md5_dict = md5_unpickler(md5_path)
except IOError:
pass
if self.hash == True:
self.cache_files(self.savepath)
else:
self.build_queue()
Traceback (most recent call last):
File "C:\Users\Cirno\Dropbox\CirnoCrawler\crawler.py", line 98, in run
self.cache_files(self.savepath)
File "C:\Users\Cirno\Dropbox\CirnoCrawler\crawler.py", line 84, in cache_files
self.md5_queue.put_nowait(filename, self.local_hash)
TypeError: put_nowait() takes exactly 2 arguments (3 given)
您可能在'put_nowait'方法的参数列表中缺少'自我'。 我相信这样的事情:
class md5_queue:
def put_nowait(filename, local_hash):
.
.
.
将其更改为以下应该可以解决您的问题:
class md5_queue:
def put_nowait(self, filename, local_hash):
.
.
.
找到了我的答案。 put_nowait()需要2个参数,put_nowait(self,(tuple,here))。 不是(元组,这里)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.