function - Kivy/Python - 使用 3 个参数调用函数而不给出任何参数

标签 function arguments kivy

这里是一本关于 Kivy 的书中的一些代码。当按下“搜索”按钮时,将调用 search_location 函数。在其 UrlRequest 中,有一个对found_location 函数的调用,不带任何参数。但found_location需要2个参数,那么它是如何工作的呢? ‘请求’在哪里? UrlRequest 中的调用不应该更像是: self.found_location(sth,sth) 吗?

class AddLocationForm(BoxLayout):
    search_input = ObjectProperty()
    search_results = ObjectProperty()
    def search_location(self):
        search_template = "http://api.openweathermap.org/data/2.5/" + "find?q={}&type=like"
        search_url = search_template.format(self.search_input.text)
        request = UrlRequest(search_url, self.found_location)
    def found_location(self, request, data):
        data = json.loads(data.decode()) if not isinstance(data, dict) else data
        cities = ["{} ({})".format(d['name'], d['sys']['country']) for d in data['list']]
        self.search_results.item_strings = cities

如果您还可以解释一下 data['list'] 是什么,那就太棒了。当我查看 search_url 链接时,里面没有“列表”。

最佳答案

UrlRequest(search_url, self.found_location)

这会向 UrlRequest 传递两个参数,其中第二个参数是函数本身。 UrlRequest 自动使用两个参数调用该函数,特别是它本身和它检索到的数据。

Shoudn't the call in UrlRequest look more like: self.found_location(sth,sth)

没有。关键是您不想调用该函数,只想将函数本身传递给 UrlRequest 以便稍后调用它。

关于function - Kivy/Python - 使用 3 个参数调用函数而不给出任何参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27035730/

相关文章:

带有打印函数结果的 Python sys.argv TypeErrors?

java - 重构java命令行参数

android - Kivy Buildozer 无法构建 apk,命令失败 : ./distribute.sh -m "kivy"d

python - 如果没有动态添加,如何正确删除 Kivy 中的小部件

c - 通过函数传递的指针和结构

c - C 程序已停止运行

python - 参数中的 RandomForest 分类器对象

python - Kivy TextInput后台问题

javascript - html5 Canvas : clearRect() not working inside function

C 程序使用函数查找两个数字之间的最大值和最小值