假设我们有以下代码。我想换掉内部的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