【问题标题】:Python throwing multiple argument error when there is only one当只有一个参数时,Python会抛出多个参数错误
【发布时间】:2020-02-09 12:12:28
【问题描述】:

我正在调用另一个类中的方法,但出现以下错误。这是声明和定义方法的类:

from web import Web
import ast


class WebCrawler:
    web = Web()

    def crawl(self):
        root = self.root()
        URL = ast.literal_eval(self.get(root))
        return URL

这是调用它的类:

from web import Web
from crawler import WebCrawler

web = Web()
crawler = WebCrawler()
urls = crawler.crawl(web)

print(urls)

但错误是这样的:

Traceback (most recent call last):
  File "/home/onur/Desktop/web-crawler/test-run.py", line 6, in <module>
    urls = crawler.crawl(web)
TypeError: crawl() takes 1 positional argument but 2 were given

【问题讨论】:

  • self 是另一个参数。除了self,您还需要接受另一个参数。
  • @VorsprungdurchTechnik 是的,但没有。我只想提供网络。

标签: python typeerror


【解决方案1】:

实例方法隐式传递实例作为第一个参数 (self)。这意味着crawler.crawl(web) 变成了WebCrawler.crawl(crawler, web)

我不知道如何解决它,因为我不熟悉这些模块,但我猜想 crawl 应该接受一个参数,因为 WebCrawler 没有 root 方法:

def crawl(self, arg):
    root = arg.root()
    ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 2013-04-10
    相关资源
    最近更新 更多