【问题标题】:method of class receiving unknown arguments接收未知参数的类方法
【发布时间】:2012-01-31 05:13:40
【问题描述】:

我有一个我定义的类,该类目前有 2 个参数,self 和 savepath。这个类的一个方法有两个参数,self。在该方法中,我调用了一个函数,该函数再次接受 2 个参数,local_hash 和文件名,但是,在调用该方法时,我得到了下面描述的错误。我认为它与 self 论点有关,但我不知道在哪里或为什么。并且为了记录, 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)

【问题讨论】:

  • 课堂上的def put_nowait()是什么?
  • put_nowait() 是 Queue 模块的一个方法。
  • 找到了我的答案。 put_nowait() 需要 2 个参数,put_nowait(self, (tuple,here))。不是put_nowait(tuple, here)

标签: python class methods


【解决方案1】:

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

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

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

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

【讨论】:

  • 信息不足怎么办? :s put_nowait 是 Queue 的一个方法,它是一个默认模块..
【解决方案2】:

找到我的答案。 put_nowait() 需要 2 个参数,put_nowait(self, (tuple,here))。不是(元组,在这里)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    • 2022-12-01
    • 2023-03-26
    • 1970-01-01
    • 2022-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多