假設我們有以下代碼。我想換掉內部的parse_place
功能。你如何在不更換整個方法的情況下做到這一點?Python多態性。你如何在一個方法中覆蓋函數
class GoogleV3Place(GoogleV3):
"""Simply extends the GoogleV3 to bucket the object into a place"""
def parse_json(self, page, exactly_one=True):
"""Returns location, (latitude, longitude) from json feed."""
if not isinstance(page, basestring):
page = util.decode_page(page)
self.doc = json.loads(page)
places = self.doc.get('results', [])
if not places:
check_status(self.doc.get('status'))
return None
elif exactly_one and len(places) != 1:
raise ValueError(
"Didn't find exactly one placemark! (Found %d)" % len(places))
def parse_place(place):
"""This returns an object how we want it returned."""
location.formatted_address = place.get('formatted_address')
location.latitude = place['geometry']['location']['lat']
location.longitude = place['geometry']['location']['lng']
latitude = place['geometry']['location']['lat']
longitude = place['geometry']['location']['lng']
return (location, (latitude, longitude))
if exactly_one:
return parse_place(places[0])
else:
return [parse_place(place) for place in places]
是的,這將工作,但(如同我對_超有限的理解),它打敗了我的問題。我只想換出解析位置方法。如果我使用超級,我需要重新定義班級。也許我正在推翻這個能給你一個例子嗎? – rh0dium 2012-02-24 16:36:12