在 Python 中将方法作为参数传递 (Scrapy) - 语法

Passing method as parameter in Python (Scrapy) - syntax

我是 Python 的新手,有一些 Java/C# 背景。我在Scrapy教程中遇到了回调语法和一个意想不到的语法,我想了解一下。

在下面代码的最后一行 parse_articles_follow_next_page 是一个方法,根据我的想象,我希望在括号中传递一个响应参数,例如: yield scrapy.Request(url, self.parse_articles_follow_next_page(someresponseobject))

那里应用了哪种 Python 语法,因此它没有括号和传递参数,我可以在哪里阅读更多相关信息?

def parse_articles_follow_next_page(self, response):
    for article in response.xpath("//article"):
        item = ArticleItem()
        #... extract article data here
        yield item

    next_page = response.css("ul.navigation > li.next-page > a::attr('href')")
    if next_page:
        url = response.urljoin(next_page[0].extract())
        yield scrapy.Request(url, self.parse_articles_follow_next_page)

来自Scrapy docs,请求的参数之一可以是回调:

callback(可调用)– 将使用此请求的响应(一旦下载)作为其第一个参数调用的函数。

所以你正在传递一个函数。响应参数将通过 Scrapy 的回调机制传递给该函数。

阅读有关将函数作为参数传递的信息: python function as a function argument?

或者一般回调:https://en.wikipedia.org/wiki/Callback_%28computer_programming%29