Reputation: 607
So here is some code from a book about Kivy. When a 'search' button is pressed search_location function is called. In its UrlRequest there is a call to found_location function without any arguments. But found_location takes 2 arguments so how does it even work? Where is the 'request'? Shoudn't the call in UrlRequest look more like: 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
If you could also explain what data['list'] is that would be awesome. When I look at the search_url link there is no 'list' inside.
Upvotes: 2
Views: 137
Reputation: 29450
UrlRequest(search_url, self.found_location)
This passes UrlRequest the two parameters, the second of which is the function itself. UrlRequest automatically calls that function with two arguments, specifically itself and the data it has retrieved.
Shoudn't the call in UrlRequest look more like: self.found_location(sth,sth)
No. The point is that you don't want to call the function, only to pass the function itself to UrlRequest so it can call it later.
Upvotes: 2