接收未知参数的类方法

时间:2011-12-31 21:31:36

标签: python class methods

我有一个我定义的类,该类目前有2个参数,self和savepath。这个类的方法有2个参数,self。在该方法中,我调用一个函数,它再次接受2个参数,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():

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)

2 个答案:

答案 0 :(得分:0)

你可能在'put_nowait'方法的参数列表中缺少'self'。我相信这样的事情:

class md5_queue:
    def put_nowait(filename, local_hash):
     .
     .
     .

将其更改为以下内容可以解决您的问题:

class md5_queue:
    def put_nowait(self, filename, local_hash):
     .
     .
     .

答案 1 :(得分:0)

找到我的答案。 put_nowait()期待2个参数,put_nowait(self,(tuple,here))。不(元组,这里)。