[Tutor] calling a superclass method after overriding it
Dave Angel
davea at ieee.org
Wed Sep 23 02:28:05 CEST 2009
Serdar Tumgoren wrote:
>> def add_name(self):
>> try:
>> self.name = SPECIALIZED_SQLcall_for_child()
>> except SpecialSQLError:
>> #default to the superclass's add_name method
>> super(Child, self).add_name()
>>
>>
> That certainly is a lot easier to read. So if I were to go that route,
> would my "SPECIALIZED_SQLCall_for_child()" function (I've slightly
> renamed it) have to resemble something like below?:
>
> def SpecialSQLcall_for_child():
> result = execute some sql_for_special_case()
> if type(result) != str:
> raise SpecialSQLError
> else:
> return result
>
>
Exactly. The whole point of forgiveness/permission is to be able to
propagate the error without having other code in the path affected by
it. So if you return at all, return a real value. (Raise isn't exactly
returning, the way I see it, since the flow of control goes elsewhere)
BTW, it's possible that "execute_some_sql...()" might itself be raising
the exception. In this case, we might make this call even simpler; it
needn't even know anything about the exception.
DaveA
More information about the Tutor
mailing list